commented project_stats
This commit is contained in:
parent
2aecc7f673
commit
e42b991fef
@ -3,64 +3,54 @@ from qiskit import (
|
|||||||
QuantumCircuit,
|
QuantumCircuit,
|
||||||
execute,
|
execute,
|
||||||
Aer)
|
Aer)
|
||||||
from qiskit.visualization import plot_histogram, plot_bloch_vector
|
|
||||||
from matplotlib import pyplot as plt
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
|
CASE_IDENTICAL = "identical"
|
||||||
|
CASE_ORTHOGONAL = "orthogonal"
|
||||||
|
|
||||||
|
|
||||||
|
def perform_exp(iterations, case):
|
||||||
# Use Aer's qasm_simulator
|
# Use Aer's qasm_simulator
|
||||||
simulator = Aer.get_backend('qasm_simulator')
|
simulator = Aer.get_backend('qasm_simulator')
|
||||||
|
|
||||||
|
|
||||||
def create_bloch_vector(theta, phi):
|
|
||||||
x = np.sin(theta) * np.cos(phi)
|
|
||||||
y = np.sin(theta) * np.sin(phi)
|
|
||||||
z = np.cos(theta)
|
|
||||||
return [x, y, z]
|
|
||||||
|
|
||||||
|
|
||||||
def perform_exp(iterations, case, draw=False):
|
|
||||||
simulator = Aer.get_backend('qasm_simulator')
|
|
||||||
all_counts = defaultdict(int)
|
all_counts = defaultdict(int)
|
||||||
|
|
||||||
for i in range(iterations):
|
for i in range(iterations):
|
||||||
qc = QuantumCircuit(2, 2)
|
qc = QuantumCircuit(2, 2)
|
||||||
|
|
||||||
# uniform distribution on the sphere
|
# produce angles with uniform distribution on the sphere
|
||||||
t = round(np.random.uniform(0, 1), 10)
|
t = round(np.random.uniform(0, 1), 10)
|
||||||
theta = np.arccos(1 - 2 * t)
|
theta0 = np.arccos(1 - 2 * t)
|
||||||
phi = round(np.random.uniform(0, 2 * np.pi), 10)
|
phi0 = round(np.random.uniform(0, 2 * np.pi), 10)
|
||||||
|
|
||||||
CASE_ANGLES = {
|
# rotate the 0th qubit in (theta, phi)
|
||||||
'identical': (theta, phi),
|
qc.r(theta0, phi0, 0)
|
||||||
'orthogonal': (theta + np.pi, phi)
|
|
||||||
}
|
|
||||||
|
|
||||||
qc.r(theta, phi, 0)
|
# rotate the 1st qubit depending on the case we are exploring...
|
||||||
theta1, phi1 = CASE_ANGLES[case]
|
if case == CASE_IDENTICAL:
|
||||||
|
# ... for identical we are rotating the 1st qubit the same angles as
|
||||||
|
# the 0th
|
||||||
|
theta1, phi1 = theta0, phi0
|
||||||
|
elif case == CASE_ORTHOGONAL:
|
||||||
|
# for orthogonal we rotate the 1st qubit in 90 degrees
|
||||||
|
# orthogonal to the first
|
||||||
|
theta1, phi1 = theta0 + np.pi, phi0
|
||||||
|
|
||||||
|
# perform the rotation of the 1st qubit
|
||||||
qc.r(theta1, phi1, 1)
|
qc.r(theta1, phi1, 1)
|
||||||
|
|
||||||
# print(theta, phi, phi1)
|
|
||||||
|
|
||||||
if draw:
|
|
||||||
q0 = create_bloch_vector(theta, phi)
|
|
||||||
plot_bloch_vector(q0)
|
|
||||||
plt.show()
|
|
||||||
q1 = create_bloch_vector(theta1, phi1)
|
|
||||||
plot_bloch_vector(q1)
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
# Measure in the Bell's basis
|
# Measure in the Bell's basis
|
||||||
qc.cx(0, 1)
|
qc.cx(0, 1)
|
||||||
qc.h(0)
|
qc.h(0)
|
||||||
qc.measure([0, 1], [0, 1])
|
|
||||||
|
|
||||||
# print(qc)
|
# measuring maps the 0/1 qubit register to the 0/1 classical register
|
||||||
|
qc.measure([0, 1], [0, 1])
|
||||||
|
|
||||||
# execute the job
|
# execute the job
|
||||||
job = execute(qc, simulator, shots=1)
|
job = execute(qc, simulator, shots=1)
|
||||||
result = job.result()
|
result = job.result()
|
||||||
counts = result.get_counts(qc)
|
counts = result.get_counts(qc)
|
||||||
|
|
||||||
|
# update the counts of all experiments with the current run
|
||||||
for k, v in counts.items():
|
for k, v in counts.items():
|
||||||
all_counts[k] += v
|
all_counts[k] += v
|
||||||
|
|
||||||
@ -71,7 +61,5 @@ def perform_exp(iterations, case, draw=False):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
# draw produces a Bloch sphere but does not work in this notebook
|
perform_exp(iterations=100, case=CASE_IDENTICAL)
|
||||||
|
perform_exp(iterations=100, case=CASE_ORTHOGONAL)
|
||||||
perform_exp(iterations=100, case='identical', draw=False) # same state
|
|
||||||
perform_exp(iterations=100, case='orthogonal', draw=False) # different orthogonal states
|
|
||||||
|
Loading…
Reference in New Issue
Block a user