experiments with qiskit

This commit is contained in:
Kristina Kirova 2020-02-01 20:45:05 +00:00
parent 911d58bfde
commit 1106bc3d42
4 changed files with 93 additions and 49 deletions

3
.gitignore vendored
View File

@ -1,3 +1,4 @@
venv
.idea
__pycache__
__pycache__
*.png

View File

@ -7,75 +7,86 @@ Created on Fri Jan 31 13:37:47 2020
"""
import numpy as np
from qiskit import(
QuantumCircuit,
execute,
Aer)
from qiskit.visualization import plot_histogram
from qiskit import (
QuantumCircuit,
execute,
Aer)
from qiskit.visualization import plot_histogram, plot_bloch_vector
from matplotlib import pyplot as plt
from scipy.stats import unitary_group
from collections import defaultdict
from qiskit.quantum_info.random.utils import random_state
# Use Aer's 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=100):
simulator = Aer.get_backend('qasm_simulator')
all_counts = defaultdict(int)
for i in range(iterations):
qc = QuantumCircuit(2, 2)
# uniform distribution on the sphere
t = round(np.random.uniform(0, 1), 10)
theta = np.arccos(1-2*t)
phi = round(np.random.uniform(0, 2*np.pi), 10)
theta = np.arccos(1 - 2 * t)
phi = round(np.random.uniform(0, 2 * np.pi), 10)
# 0_case, identical states
# qc.r(theta, phi, 0)
# qc.r(theta, phi, 1)
# qc.r(theta, phi, 0)
# qc.r(theta, phi, 1)
# 1_case, orthogonal states on the same meridian
# qc.r(theta, phi, 0)
# qc.r(theta+np.pi, phi, 1)
# qc.r(theta, phi, 0)
# qc.r(theta+np.pi, phi, 1)
# my_case, phase difference DOESNT WORK
phi1 = 2 * np.pi - phi
qc.r(theta, phi, 0)
qc.r(theta, 2*np.pi - phi, 1)
qc.r(theta, phi1, 1)
# print(theta, phi, phi1)
# q0 = create_bloch_vector(theta, phi)
# plot_bloch_vector(q0)
# plt.show()
#
# q1 = create_bloch_vector(theta, phi1)
# plot_bloch_vector(q1)
# plt.show()
# Measure in the Bell's basis
qc.cx(0, 1)
qc.h(0)
# qc.measure_all()
# qc.measure_all()
qc.measure([0, 1], [0, 1])
# print(qc)
# print(qc)
job = execute(qc, simulator, shots=1)
result = job.result()
#print(result)
# print(result)
counts = result.get_counts(qc)
# print(counts.keys())
# print(counts.keys())
for k, v in counts.items():
all_counts[k] += v
print("\nTotal counts are:\n")
for k, v in sorted(all_counts.items(), key=lambda x: x[0]):
print("{}: {}".format(k, v))
if __name__ == "__main__":
perform_exp()

View File

@ -7,25 +7,26 @@ Created on Fri Jan 31 13:37:47 2020
"""
import numpy as np
from qiskit import(
QuantumCircuit,
execute,
Aer)
from qiskit import (
QuantumCircuit,
execute,
Aer)
from qiskit.visualization import plot_histogram
from scipy.stats import unitary_group
from collections import defaultdict
def perform_exp(iterations=1000):
simulator = Aer.get_backend('qasm_simulator')
all_counts = defaultdict(int)
for i in range(iterations):
qc = QuantumCircuit(2, 2)
t = round(np.random.uniform(0, 1), 10)
phi = round(np.random.uniform(0, 2*np.pi), 10)
theta = np.arccos(1-2*t)
phi = round(np.random.uniform(0, 2 * np.pi), 10)
theta = np.arccos(1 - 2 * t)
qc.r(theta, phi, 0)
qc.r(theta, -phi, 1)
@ -33,30 +34,25 @@ def perform_exp(iterations=1000):
# Measure in the Bell's basis
qc.cx(0, 1)
qc.h(0)
# qc.measure_all()
# qc.measure_all()
qc.measure([0, 1], [0, 1])
# print(qc)
# print(qc)
job = execute(qc, simulator, shots=1)
result = job.result()
#print(result)
# print(result)
counts = result.get_counts(qc)
# print(counts.keys())
# print(counts.keys())
for k, v in counts.items():
all_counts[k] += v
print("\nTotal counts are:\n")
for k, v in sorted(all_counts.items(), key=lambda x: x[0]):
print("{}: {}".format(k, v))
if __name__ == "__main__":
perform_exp()

View File

@ -1,41 +1,75 @@
attrs==19.3.0
backcall==0.1.0
cachetools==3.1.1
certifi==2019.9.11
cffi==1.13.2
chardet==3.0.4
cirq==0.7.0
cryptography==2.8
cvxopt==1.2.4
cycler==0.10.0
Cython==0.29.14
dataclasses==0.7
decorator==4.4.0
dill==0.3.1.1
dlx==1.0.4
docloud==1.0.375
docplex==2.12.182
fastdtw==0.3.4
google-api-python-client==1.7.11
google-auth==1.6.3
google-auth-httplib2==0.0.3
h5py==2.10.0
httplib2==0.13.1
idna==2.8
importlib-metadata==1.5.0
inflection==0.3.1
ipython==7.8.0
ipython-genutils==0.2.0
jedi==0.15.1
joblib==0.14.1
jsonschema==3.2.0
kiwisolver==1.1.0
llvmlite==0.31.0
marshmallow==3.3.0
marshmallow-polyfield==5.8
matplotlib==3.1.2
more-itertools==8.2.0
mpmath==1.1.0
nest-asyncio==1.2.3
networkx==2.3
ntlm-auth==1.4.0
numba==0.47.0
numpy==1.17.2
pandas==0.25.3
parso==0.5.1
pexpect==4.7.0
pickleshare==0.7.5
ply==3.11
prompt-toolkit==2.0.9
protobuf==3.8.0
psutil==5.6.7
ptyprocess==0.6.0
pyasn1==0.4.7
pyasn1-modules==0.2.6
pycparser==2.19
Pygments==2.4.2
pyparsing==2.4.2
pyrsistent==0.15.7
pyscf==1.7.0
python-dateutil==2.8.0
pytz==2019.2
qiskit==0.14.1
qiskit-aer==0.3.4
qiskit-aqua==0.6.2
qiskit-ibmq-provider==0.4.5
qiskit-ignis==0.2.0
qiskit-terra==0.11.1
Quandl==3.5.0
requests==2.22.0
requests-ntlm==1.1.0
rsa==4.0
scikit-learn==0.22.1
scipy==1.3.1
six==1.12.0
sortedcontainers==2.1.0
@ -45,3 +79,5 @@ typing-extensions==3.7.4
uritemplate==3.0.0
urllib3==1.25.6
wcwidth==0.1.7
websockets==7.0
zipp==2.1.0