import numpy as np
from qiskit import *
from qiskit.tools.jupyter import *
%matplotlib inline

IBMQ.load_account()

provider = IBMQ.get_provider(group='open')
provider.backends(simulator=False)

backend = provider.get_backend('ibmqx4')
backend.configuration().basis_gates

qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(1, 'c')
qc = QuantumCircuit(qr, cr)

qc.h(qr[0])
qc.x(qr[1])
qc.cu1(np.pi/4, qr[0], qr[1])
qc.h(qr[0])
qc.measure(qr[0], cr[0])
qc.draw(output='mpl')

qr = QuantumRegister(2, 'q')
cr = ClassicalRegister(1, 'c')
qc_basis = QuantumCircuit(qr, cr)

# Hadamard in U2 format
qc_basis.u2(0, np.pi, qr[0])
# X gate in U3 format
qc_basis.u3(np.pi, 0, np.pi, qr[1])

# Decomposition for controlled-U1 with lambda=pi/4
qc_basis.u1(np.pi/8, qr[0]) 
qc_basis.cx(qr[0], qr[1]) 
qc_basis.u1(-np.pi/8, qr[1]) 
qc_basis.cx(qr[0], qr[1])
qc_basis.u1(np.pi/8, qr[1])

# Hadamard in U2 format
qc_basis.u2(0, np.pi, qr[0]) 

qc_basis.measure(qr[0], cr[0])
qc_basis.draw(output='mpl')

print(qc.depth(), ',', qc_basis.depth())

qr = QuantumRegister(3, 'q')
qc = QuantumCircuit(qr)

qc.ccx(qr[0], qr[1], qr[2])
qc.draw(output='mpl')

qr = QuantumRegister(3, 'q')
qc_basis = QuantumCircuit(qr)

qc_basis.u2(0,np.pi, qr[2])
qc_basis.cx(qr[1], qr[2])
qc_basis.u1(-np.pi/4, qr[2])
qc_basis.cx(qr[0], qr[2])
qc_basis.u1(np.pi/4, qr[2])
qc_basis.cx(qr[1], qr[2])
qc_basis.u1(np.pi/4, qr[1])
qc_basis.u1(-np.pi/4, qr[2])
qc_basis.cx(qr[0], qr[2])
qc_basis.cx(qr[0], qr[1])
qc_basis.u1(np.pi/4, qr[2])
qc_basis.u1(np.pi/4, qr[0])
qc_basis.u1(-np.pi/4, qr[1])
qc_basis.u2(0,np.pi, qr[2])
qc_basis.cx(qr[0], qr[1])
qc_basis.draw(output='mpl')

qr = QuantumRegister(5, 'q')
cr = ClassicalRegister(5, 'c')
qc = QuantumCircuit(qr, cr)

qc.h(qr[0])
qc.cx(qr[0], qr[4])
qc.cx(qr[4], qr[3])
qc.cx(qr[3], qr[1])
qc.cx(qr[1], qr[2])

qc.draw(output='mpl')

backend = provider.get_backend('ibmqx4')

from qiskit.visualization.gate_map import plot_gate_map
plot_gate_map(backend, plot_directed=True)