In [1]:
# Checking the version of PYTHON; we only support 3 at the moment
import sys
if sys.version_info < (3,0):
raise Exception("Please use Python version 3 or greater.")
# useful additional packages
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
from scipy import linalg as la
import sys
sys.path.append("../../")
# importing the QISKit
from qiskit import QuantumCircuit, QuantumProgram
import Qconfig
In [2]:
# import tomography libary
import qiskit.tools.qcvv.tomography as tomo
# useful additional packages
from qiskit.tools.visualization import plot_state, plot_histogram
from qiskit.tools.qi.qi import state_fidelity, concurrence, purity
In [3]:
qp = QuantumProgram()
qp.set_api(Qconfig.APItoken, Qconfig.config["url"], verify=False) # set the APIToken and API url
In [4]:
qp.available_backends()
Out[4]:
In [5]:
conf = qp.get_backend_configuration("ibmqx2", list_format=True)
In [6]:
backend = conf['name']
shots = 1000
max_credits = 5
n = conf['n_qubits']
In [7]:
#Setup the experiment run one circuit
q = qp.create_quantum_register("q", n)
c = qp.create_classical_register("c", n)
bell = qp.create_circuit("bell", [q], [c])
bell.h(q[0])
bell.cx(q[0], q[1])
bell.measure(q[0], c[0])
bell.measure(q[1], c[1])
circuits = ["bell"]
result = qp.execute(circuits, backend, shots=shots, max_credits=max_credits, wait=10, timeout=240)
plot_histogram(result.get_counts("bell"))
In [8]:
# run tomograph
bell_tomo = qp.create_circuit("bell_tomo", [q], [c])
bell_tomo.h(q[0])
bell_tomo.cx(q[0], q[1])
meas_qubits= [0,1]
tomo_labels = tomo.build_state_tomography_circuits(qp, "bell_tomo", meas_qubits, q, c)
out = qp.execute(tomo_labels, backend, shots=shots, max_credits=max_credits, wait=20, timeout=20)
if str(out.get_error()) == 'Time Out':
print('Timed out, trying again')
out = qp.execute(tomo_labels, backend, shots=shots, max_credits=max_credits, wait=40, timeout=400)
if str(out) !='ERROR':
target = np.array([1., 0., 0.,1.]/np.sqrt(2.))
bell_tomo_dat = tomo.state_tomography_data(out, 'bell_tomo', meas_qubits)
rho_fit = tomo.fit_tomography_data(bell_tomo_dat, method='wizard')
F_fit = state_fidelity(rho_fit, target)
print("fid = " + str(F_fit))
plot_state(rho_fit,"paulivec")
In [12]:
# run all gates
for i in conf['coupling_map']:
meas_qubits = i
bell_tomo = qp.create_circuit("bell_tomo", [q], [c])
bell_tomo.h(q[i[0]])
bell_tomo.cx(q[i[0]], q[i[1]])
tomo_labels = tomo.build_state_tomography_circuits(qp, "bell_tomo", meas_qubits, q, c)
out = qp.execute(tomo_labels, backend, shots=shots, max_credits=max_credits, wait=20, timeout=400)
if str(out.get_error()) == 'Time Out':
print('Timed out, trying again')
out = qp.execute(tomo_labels, backend, shots=shots, max_credits=max_credits, wait=40, timeout=400)
if str(out) !='ERROR':
target = np.array([1., 0., 0.,1.]/np.sqrt(2.))
bell_tomo_dat = tomo.state_tomography_data(out, 'bell_tomo', meas_qubits)
rho_fit = tomo.fit_tomography_data(bell_tomo_dat, method='wizard')
F_fit = state_fidelity(rho_fit, target)
print("fid = " + str(F_fit))
plot_state(rho_fit,"paulivec")
In [17]:
qp = QuantumProgram()
qp.set_api(Qconfig.APItoken, Qconfig.config["url"], verify=False) # set the APIToken and API url
conf = qp.get_backend_configuration("ibmqx3", list_format=True)
conf
Out[17]:
In [18]:
backend = conf['name']
shots = 1000
max_credits = 5
n = conf['n_qubits']
In [19]:
#Setup the experiment run one circuit
q = qp.create_quantum_register("q", n)
c = qp.create_classical_register("c", n)
bell = qp.create_circuit("bell", [q], [c])
bell.h(q[0])
bell.cx(q[0], q[1])
bell.measure(q[0], c[0])
bell.measure(q[1], c[1])
circuits = ["bell"]
result = qp.execute(circuits, backend, shots=shots, max_credits=max_credits, wait=10, timeout=240)
plot_histogram(result.get_counts("bell"))
In [20]:
# run tomograph
bell_tomo = qp.create_circuit("bell_tomo", [q], [c])
bell_tomo.h(q[0])
bell_tomo.cx(q[0], q[1])
meas_qubits= [0,1]
tomo_labels = tomo.build_state_tomography_circuits(qp, "bell_tomo", meas_qubits, q, c)
out = qp.execute(tomo_labels, backend, shots=shots, max_credits=max_credits, wait=20, timeout=20)
if str(out.get_error()) == 'Time Out':
print('Timed out, trying again')
out = qp.execute(tomo_labels, backend, shots=shots, max_credits=max_credits, wait=40, timeout=400)
if str(out) !='ERROR':
target = np.array([1., 0., 0.,1.]/np.sqrt(2.))
bell_tomo_dat = tomo.state_tomography_data(out, 'bell_tomo', meas_qubits)
rho_fit = tomo.fit_tomography_data(bell_tomo_dat, method='wizard')
F_fit = state_fidelity(rho_fit, target)
print("fid = " + str(F_fit))
plot_state(rho_fit,"paulivec")
In [21]:
# run all gates
for i in conf['coupling_map']:
meas_qubits = i
bell_tomo = qp.create_circuit("bell_tomo", [q], [c])
bell_tomo.h(q[i[0]])
bell_tomo.cx(q[i[0]], q[i[1]])
tomo_labels = tomo.build_state_tomography_circuits(qp, "bell_tomo", meas_qubits, q, c)
out = qp.execute(tomo_labels, backend, shots=shots, max_credits=max_credits, wait=20, timeout=400)
if str(out.get_error()) == 'Time Out':
print('Timed out, trying again')
out = qp.execute(tomo_labels, backend, shots=shots, max_credits=max_credits, wait=40, timeout=400)
if str(out) !='ERROR':
target = np.array([1., 0., 0.,1.]/np.sqrt(2.))
bell_tomo_dat = tomo.state_tomography_data(out, 'bell_tomo', meas_qubits)
rho_fit = tomo.fit_tomography_data(bell_tomo_dat, method='wizard')
F_fit = state_fidelity(rho_fit, target)
print("fid = " + str(F_fit))
plot_state(rho_fit,"paulivec")
In [ ]: