from IPython.display import Image


Image(filename="error_correction_files/error_correction_1_0.png", width=450, height=300)

Image(filename="error_correction_files/error_correction_3_0.png", width=450, height=300)   

Image(filename="error_correction_files/error_correction_5_0.png", width=450, height=300)  

Image(filename="error_correction_files/error_correction_7_0.png", width=450, height=300)    

import sys
if sys.version_info < (3,5):
    raise Exception('Please use Python version 3.5 or greater.')
    
from qiskit import QuantumProgram
#import Qconfig    

# Needed to visualize quantum circuits
import os
import shutil
from qiskit.tools.visualization import latex_drawer
import pdf2image

from IPython.display import Image

# Initialize quantum program
qp = QuantumProgram()
#qp.set_api(Qconfig.APItoken, Qconfig.config['url']) # set the APIToken and API url   

# Circuit requires 5 qubits and 5 classical bits
qr = qp.create_quantum_register('qr', 5)
cr = qp.create_classical_register('cr',5)

qc = qp.create_circuit('Circuit', [qr], [cr])
circuit = qp.get_circuit('Circuit')
    
# get the Quantum Register by Name
quantum_r = qp.get_quantum_register('qr')

# get the Classical Register by Name
classical_r = qp.get_classical_register('cr')

def circuitImage(circuit, filename, basis="u1,u2,u3,cx"):
    """
    Obtain the circuit in image format
    Note: Requires pdflatex installed (to compile Latex)
    Note: Required pdf2image Python package (to display pdf as image)
    """
    tmpdir='tmp/'
    if not os.path.exists(tmpdir):
        os.makedirs(tmpdir)
    latex_drawer(circuit, tmpdir+filename+".tex", basis=basis)
    os.system("pdflatex -output-directory {} {}".format(tmpdir, filename+".tex"))
    images = pdf2image.convert_from_path(tmpdir+filename+".pdf")
    shutil.rmtree(tmpdir)
    return images[0]

def toffoli(circuit,quantum_r,a,b,c):
    """
    Creates toffoli gate in existing circuit with a and b 
    as the test points and c as the affected point
    """
    circuit.iden(quantum_r[c])
    circuit.h(quantum_r[c])
    circuit.cx(quantum_r[b],quantum_r[c])
    circuit.tdg(quantum_r[c])
    circuit.cx(quantum_r[a],quantum_r[c])
    circuit.t(quantum_r[c])
    circuit.cx(quantum_r[b],quantum_r[c])
    circuit.tdg(quantum_r[c])
    circuit.cx(quantum_r[a],quantum_r[c])
    circuit.t(quantum_r[c])
    circuit.t(quantum_r[b])
    circuit.h(quantum_r[c])
    circuit.cx(quantum_r[b],quantum_r[c])
    circuit.h(quantum_r[c])
    circuit.h(quantum_r[b])
    circuit.cx(quantum_r[b],quantum_r[c])
    circuit.h(quantum_r[c])
    circuit.h(quantum_r[b])
    circuit.cx(quantum_r[b],quantum_r[c])
    circuit.cx(quantum_r[a],quantum_r[c])
    circuit.t(quantum_r[a])
    circuit.tdg(quantum_r[c])
    circuit.cx(quantum_r[a],quantum_r[c])
    circuit.cx(quantum_r[b],quantum_r[c])
    circuit.cx(quantum_r[c],quantum_r[b])
    circuit.cx(quantum_r[b],quantum_r[c])



#circuit.x(quantum_r[2])
circuit.x(quantum_r[3])
circuit.x(quantum_r[4])

circuit.cx(quantum_r[2],quantum_r[0])
circuit.cx(quantum_r[3],quantum_r[0])
circuit.cx(quantum_r[3],quantum_r[1])
circuit.cx(quantum_r[4],quantum_r[1])

circuit.cx(quantum_r[0],quantum_r[2])
circuit.cx(quantum_r[1],quantum_r[4])
toffoli(circuit,quantum_r,0,1,2)
toffoli(circuit,quantum_r,0,1,3)
toffoli(circuit,quantum_r,0,1,4)

circuit.measure(quantum_r[0], classical_r[0])
circuit.measure(quantum_r[1], classical_r[1])
circuit.measure(quantum_r[2], classical_r[2])
circuit.measure(quantum_r[3], classical_r[3])
circuit.measure(quantum_r[4], classical_r[4])

"""
Image of the final circuit. Becuase it contains three Toffoli gates where
each gate is made up of many basis gates, this circuit is unfortunately 
very hard to visualize. 
"""
basis="u1,u2,u3,cx,x,y,z,h,s,t,rx,ry,rz"
circuitImage(circuit,'circuit',basis)

#!!! for better visibility plot using the now built-in code
from qiskit.tools.visualization import circuit_drawer 
circuit_drawer(circuit)

"""
Results of the computation. Note that the states of the five qubits
from up to down in the circuit are shown from right to left in the result. 
"""

backend = 'ibmqx_qasm_simulator'   
circuits = ['Circuit']  # Group of circuits to execute
qobj=qp.compile(circuits, backend, shots=1024, max_credits=3)
result = qp.run(qobj, wait=2, timeout=240)
print(result.get_counts('Circuit'))

Image(filename="error_correction_files/error_correction_27_0.png", width=250, height=300)   

Image(filename="error_correction_files/error_correction_30_0.png", width=450, height=300)

Image(filename="error_correction_files/error_correction_33_0.png", width=450, height=300)

Image(filename="error_correction_files/error_correction_36_0.png", width=450, height=300)

Image(filename="error_correction_files/error_correction_39_0.png", width=900, height=600)

Image(filename="error_correction_files/error_correction_40_0.png", width=900, height=600)

Image(filename="error_correction_files/error_correction_43_0.png", width=900, height=450)

