from math import pi
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from qiskit.tools.visualization import matplotlib_circuit_drawer as drawer, qx_color_scheme

# We recommend the following options for Jupter notebook
%matplotlib inline
%config InlineBackend.figure_format = 'svg'

# Create a Quantum Register called "q" with 3 qubits
qr = QuantumRegister(3, 'q')

# Create a Classical Register called "c" with 3 bits
cr = ClassicalRegister(3, 'c')

# Create a Quantum Circuit called involving "qr" and "cr"
circuit = QuantumCircuit(qr, cr)

circuit.x(qr[0]).c_if(cr, 3)
circuit.z(qr[0])
circuit.u2(pi/2, 2*pi/3, qr[1])

circuit.cu1(pi, qr[0], qr[1])

# Barrier to seperator the input from the circuit
circuit.barrier(qr[0])
circuit.barrier(qr[1])
circuit.barrier(qr[2])

# Toffoli gate from qubit 0,1 to qubit 2
circuit.ccx(qr[0], qr[1], qr[2])

# CNOT (Controlled-NOT) gate from qubit 0 to qubit 1
circuit.cx(qr[0], qr[1])

circuit.swap(qr[0], qr[2])

# measure gate from qr to cr
circuit.measure(qr, cr)

QASM_source = circuit.qasm()
print(QASM_source)

drawer(circuit)

drawer(circuit, basis='u1,u2,u3,id,cx', scale=1.0)

my_style = {'plotbarrier': True}
drawer(circuit, style=my_style)

my_style = {'cregbundle': True}
drawer(circuit, style=my_style)

my_style = {'showindex': True}
drawer(circuit, style=my_style)

my_style = {'compress': True}
drawer(circuit, style=my_style)

my_style = {'fold': 6}
drawer(circuit, style=my_style)

my_style = {'usepiformat': True}
drawer(circuit, style=my_style)

qr = QuantumRegister(1, 'q')
circuit_xyz = QuantumCircuit(qr)
circuit_xyz.x(qr[0])
circuit_xyz.y(qr[0])
circuit_xyz.z(qr[0])
drawer(circuit_xyz)

my_style = {'displaytext': {'x': '😺', 'y': '\Sigma', 'z': '✈'}}
drawer(circuit_xyz, style=my_style)

qr = QuantumRegister(2, 'q')
circuit_cucz = QuantumCircuit(qr)
circuit_cucz.cz(qr[0], qr[1])
circuit_cucz.cu1(pi, qr[0], qr[1])
drawer(circuit_cucz)

my_style = {'latexdrawerstyle': False}
drawer(circuit_cucz, style=my_style)

qr = QuantumRegister(3, 'q')
cr = ClassicalRegister(3, 'c')
circuit_all = QuantumCircuit(qr, cr)

circuit_all.x(qr[0])
circuit_all.y(qr[0])
circuit_all.z(qr[0])
circuit_all.barrier(qr[0])
circuit_all.barrier(qr[1])
circuit_all.barrier(qr[2])
circuit_all.h(qr[0])
circuit_all.s(qr[0])
circuit_all.sdg(qr[0])
circuit_all.t(qr[0])
circuit_all.tdg(qr[0])
circuit_all.iden(qr[0])
circuit_all.reset(qr[0])
circuit_all.rx(pi, qr[0])
circuit_all.ry(pi, qr[0])
circuit_all.rz(pi, qr[0])
circuit_all.u0(pi, qr[0])
circuit_all.u1(pi, qr[0])
circuit_all.u2(pi, pi, qr[0])
circuit_all.u3(pi, pi, pi, qr[0])
circuit_all.swap(qr[0], qr[1])
circuit_all.cx(qr[0], qr[1])
circuit_all.cy(qr[0], qr[1])
circuit_all.cz(qr[0], qr[1])
circuit_all.ch(qr[0], qr[1])
circuit_all.cu1(pi, qr[0], qr[1])
circuit_all.cu3(pi, pi, pi, qr[0], qr[1])
circuit_all.crz(pi, qr[0], qr[1])
circuit_all.ccx(qr[0], qr[1], qr[2])
circuit_all.cswap(qr[0], qr[1], qr[2])

circuit_all.measure(qr, cr)

drawer(circuit_all)

cmp_style = qx_color_scheme()
cmp_style

drawer(circuit_all, style=cmp_style)

cmp_style.update({
    'usepiformat': True,
    'showindex': True,
    'cregbundle': True,
    'compress': True,
    'fold': 17
})
drawer(circuit_all, filename='circuit.pdf', style=cmp_style)

