Krisis struggles

This commit is contained in:
Kristina Kirova 2020-02-01 10:46:55 +00:00
parent 5266040972
commit efa4ecbb6c
6 changed files with 246 additions and 10 deletions

View File

@ -13,6 +13,15 @@ def from_angles_1(theta, phi):
return State(m.m)
def from_angles_2(theta, phi):
# phase difference
theta, phi = State._normalize_angles(theta, phi)
m0 = np.cos(theta / 2)
m1 = np.sin(theta / 2) * np.power(np.e, (1j * (-phi)))
m = m0 * _0 + m1 * _1
return State(m.m)
def print_all_samples(name, all_samples):
print("------------- ALL SAMPLES for {}".format(name))
for k, v in sorted(all_samples.items(), key=lambda x: x[0]):
@ -20,12 +29,16 @@ def print_all_samples(name, all_samples):
print("==============================================")
def math_sim(q2func=from_angles_1, iterations=1000, sample_count=1):
def math_sim(q2func=State.from_angles, iterations=1000, sample_count=1):
all_samples = defaultdict(int)
for i in range(iterations):
# print("Running iteration {}".format(i))
theta = round(np.random.uniform(0, np.pi), 3)
phi = round(np.random.uniform(0, 2 * np.pi), 3)
# theta = round(np.random.uniform(0, np.pi), 3)
# phi = round(np.random.uniform(0, 2 * np.pi), 3)
t = round(np.random.uniform(0, 1), 10)
phi = round(np.random.uniform(0, 2 * np.pi), 10)
theta = np.arccos(1 - 2 * t)
# print("theta: {:.2f} ({:.2f} deg) | phi: {:.2f} ({:.2f} deg)".format(
# theta, np.rad2deg(theta),
# phi, np.rad2deg(phi)))
@ -63,15 +76,16 @@ class MemoizedExp(object):
def gen_exp_for_cirq_0():
theta = round(np.random.uniform(0, 1), 10)
phi = round(np.random.uniform(0, 1), 10)
t = round(np.random.uniform(0, 1), 10)
phi = round(np.random.uniform(0, 2), 10)
theta = np.arccos(1 - 2 * t) / (2 * np.pi)
return theta, phi
def gen_exp_for_cirq_1():
"""TODO: How to generate the exponents for the second case"""
theta = round(np.random.uniform(0, 1), 10)
phi = round(np.random.uniform(0, 1), 10)
phi = round(np.random.uniform(0, 2), 10)
return theta, phi
@ -119,6 +133,10 @@ def math_sim_1(*args, **kwargs):
math_sim(q2func=from_angles_1, *args, **kwargs)
def math_sim_2(*args, **kwargs):
math_sim(q2func=from_angles_2, *args, **kwargs)
def cirq_sim_0(*args, **kwargs):
cirq_sim(q1func=gen_exp_for_cirq_0,
q2func=gen_exp_for_cirq_0,
@ -132,5 +150,6 @@ def cirq_sim_1(*args, **kwargs):
if __name__ == "__main__":
math_sim_0()
cirq_sim_0()
# math_sim_0(iterations=5000)
math_sim_2(iterations=500)
# cirq_sim_0()

81
07_project_qiskit.py Normal file
View File

@ -0,0 +1,81 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 31 13:37:47 2020
@author: kidiki
"""
import numpy as np
from qiskit import(
QuantumCircuit,
execute,
Aer)
from qiskit.visualization import plot_histogram
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 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)
# 0_case, identical states
# 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)
# my_case, phase difference DOESNT WORK
qc.r(theta, phi, 0)
qc.r(theta, 2*np.pi - phi, 1)
# Measure in the Bell's basis
qc.cx(0, 1)
qc.h(0)
# qc.measure_all()
qc.measure([0, 1], [0, 1])
# print(qc)
job = execute(qc, simulator, shots=1)
result = job.result()
#print(result)
counts = result.get_counts(qc)
# 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

@ -0,0 +1,62 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 31 13:37:47 2020
@author: kidiki
"""
import numpy as np
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)
qc.r(theta, phi, 0)
qc.r(theta, -phi, 1)
# Measure in the Bell's basis
qc.cx(0, 1)
qc.h(0)
# qc.measure_all()
qc.measure([0, 1], [0, 1])
# print(qc)
job = execute(qc, simulator, shots=1)
result = job.result()
#print(result)
counts = result.get_counts(qc)
# 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

@ -0,0 +1,61 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 31 13:37:47 2020
@author: kidiki
"""
import numpy as np
from qiskit import (
QuantumCircuit,
execute,
Aer)
from qiskit.visualization import plot_histogram
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 perform_exp(iterations=3):
simulator = Aer.get_backend('qasm_simulator')
all_counts = defaultdict(int)
for i in range(iterations):
qc = QuantumCircuit(2, 2)
q0 = random_state(2)
q1 = q0
print(q0)
# Measure in the Bell's basis
qc.cx(0, 1)
qc.h(0)
# qc.measure_all()
qc.measure([0, 1], [0, 1])
# print(qc)
job = execute(qc, simulator, shots=1)
result = job.result()
# print(result)
counts = result.get_counts(qc)
# 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()

15
08_qiskit_tutorial.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 31 14:10:59 2020
@author: kidiki
"""
from qiskit import QuantumCircuit
qc = QuantumCircuit(2, 2)
qc.h(0)
qc.cx(0, 1)
qc.measure([0, 1], [0, 1])
print(qc)

View File

@ -1,5 +1,3 @@
from __future__ import annotations
import random
from collections import defaultdict
from functools import reduce