From efa4ecbb6c33756e508bbc0a79378628ccd663d2 Mon Sep 17 00:00:00 2001 From: Kristina Kirova Date: Sat, 1 Feb 2020 10:46:55 +0000 Subject: [PATCH] Krisis struggles --- 06_krisis.py | 35 ++++++++++++---- 07_project_qiskit.py | 81 +++++++++++++++++++++++++++++++++++++ 07_project_qiskit_phase.py | 62 ++++++++++++++++++++++++++++ 07_project_qiskit_random.py | 61 ++++++++++++++++++++++++++++ 08_qiskit_tutorial.py | 15 +++++++ lib_q_computer_math.py | 2 - 6 files changed, 246 insertions(+), 10 deletions(-) create mode 100644 07_project_qiskit.py create mode 100644 07_project_qiskit_phase.py create mode 100644 07_project_qiskit_random.py create mode 100644 08_qiskit_tutorial.py diff --git a/06_krisis.py b/06_krisis.py index 3a8f0bc..18161c3 100644 --- a/06_krisis.py +++ b/06_krisis.py @@ -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() diff --git a/07_project_qiskit.py b/07_project_qiskit.py new file mode 100644 index 0000000..98081f1 --- /dev/null +++ b/07_project_qiskit.py @@ -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() diff --git a/07_project_qiskit_phase.py b/07_project_qiskit_phase.py new file mode 100644 index 0000000..f40e5cd --- /dev/null +++ b/07_project_qiskit_phase.py @@ -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() diff --git a/07_project_qiskit_random.py b/07_project_qiskit_random.py new file mode 100644 index 0000000..ebfcfcf --- /dev/null +++ b/07_project_qiskit_random.py @@ -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() diff --git a/08_qiskit_tutorial.py b/08_qiskit_tutorial.py new file mode 100644 index 0000000..43f41b1 --- /dev/null +++ b/08_qiskit_tutorial.py @@ -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) diff --git a/lib_q_computer_math.py b/lib_q_computer_math.py index 800066b..9a5c437 100644 --- a/lib_q_computer_math.py +++ b/lib_q_computer_math.py @@ -1,5 +1,3 @@ -from __future__ import annotations - import random from collections import defaultdict from functools import reduce