From 717f83c981dc2c1a643a4fc3365631163ed16a46 Mon Sep 17 00:00:00 2001 From: David Cox Date: Tue, 19 Mar 2019 14:24:02 -0400 Subject: [PATCH 1/7] Add Quantum Teleportation to examples. Very simplistic Quantum Teleportation example that might serve as an instructive file. --- examples/quantum_teleportation.py | 90 +++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 examples/quantum_teleportation.py diff --git a/examples/quantum_teleportation.py b/examples/quantum_teleportation.py new file mode 100644 index 00000000000..b203c1c7475 --- /dev/null +++ b/examples/quantum_teleportation.py @@ -0,0 +1,90 @@ +"""Quantum Teleportation. +Quantum Teleportation is a process by which a quantum state can be transmitted +by sending only two classical bits of information. This is accomplished by +pre-sharing an entangled state between the sender and the receiver. This +entangled state allows the receiver of the two classical bits of information +to possess a qubit with the same state as the one held by the sender. + +In the following example output, qubit 0 is set to a random state by applying +X and Y gates. By sending two classical bits of information after qubit 0 and +qubit 1 are measured, the final state of qubit 2 will be identical to the +original random state of qubit 0. This is only possible given that an +entangled state is pre-shared between the sender and receiver. + +=== REFERENCE === +https://en.wikipedia.org/wiki/Quantum_teleportation +https://journals.aps.org/prl/abstract/10.1103/PhysRevLett.70.1895 + +=== EXAMPLE OUTPUT === +Circuit: +0: ---X^0.25---Y^0.125-----------@---H---M-------@--- + | | +1: ----------------------H---@---X-------M---@---|--- + | | | +2: --------------------------X---------------X---@--- + +Bloch Sphere of Qubit 0 After Random X and Y Gates: +x: 0.2706 y: -0.7071 z: 0.6533 + +Bloch Sphere of Qubit 2 at Final State: +x: 0.2706 y: -0.7071 z: 0.6533 + +""" + +import random +import numpy as np +import cirq + + +def make_quantum_teleportation_circuit(): + circuit = cirq.Circuit() + q0, q1, q2 = cirq.LineQubit.range(3) + + # Creates a random state for q0 + circuit.append([cirq.X(q0)**random.random(), cirq.Y(q0)**random.random()]) + # Creates Bell State to be shared on q1 and q2 + circuit.append([cirq.H(q1), cirq.CNOT(q1, q2)]) + # Bell measurement of q0 (qubit to be teleported) and q1 (entangled qubit) + circuit.append([cirq.CNOT(q0, q1), cirq.H(q0)]) + circuit.append([cirq.measure(q0), cirq.measure(q1)]) + # Uses q0 and q1 to perform operation on q2 to recover the state of q0 + circuit.append([cirq.CNOT(q1, q2), cirq.CZ(q0, q2)]) + + return circuit + + +def main(): + state = [] + circuit = make_quantum_teleportation_circuit() + + print("Circuit:") + print(circuit) + + sim = cirq.Simulator() + + # Records in a list each state of q0 for the simulation + step_results = sim.simulate_moment_steps(circuit) + for step in step_results: + state.append(cirq.bloch_vector_from_state_vector(step.state(), 0)) + + print("\nBloch Sphere of Qubit 0 After Random X and Y Gates:") + # Prints the Bloch Sphere of q0 after the X and Y gates + b0X, b0Y, b0Z = state[1] + print("x: ", np.around(b0X, 4), + "y: ", np.around(b0Y, 4), + "z: ", np.around(b0Z, 4)) + + # Records the final state of the simulation + final_results = sim.simulate(circuit) + + print("\nBloch Sphere of Qubit 2 at Final State:") + # Prints the Bloch Sphere of q2 at the final state + b2X, b2Y, b2Z = \ + cirq.bloch_vector_from_state_vector(final_results.final_state, 2) + print("x: ", np.around(b2X, 4), + "y: ", np.around(b2Y, 4), + "z: ", np.around(b2Z, 4)) + + +if __name__ == '__main__': + main() From b0eb6daab04e800f52450aee6cf18f67b9732c90 Mon Sep 17 00:00:00 2001 From: David Cox Date: Tue, 19 Mar 2019 14:27:41 -0400 Subject: [PATCH 2/7] Add Quantum Teleportation to examples_perf_test.py --- examples/examples_perf_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/examples_perf_test.py b/examples/examples_perf_test.py index 7a5f526b450..cf199d68699 100644 --- a/examples/examples_perf_test.py +++ b/examples/examples_perf_test.py @@ -8,6 +8,7 @@ import examples.bcs_mean_field import examples.phase_estimator import examples.basic_arithmetic +import examples.quantum_teleportation import examples.superdense_coding @@ -49,6 +50,8 @@ def test_example_runs_grover_perf(benchmark): def test_example_runs_phase_estimator_perf(benchmark): benchmark(examples.phase_estimator.main, qnums=(2,), repetitions=2) +def test_example_runs_quantum_teleportation(benchmark): + benchmark(examples.quantum_teleportation.main) def test_example_runs_superdense_coding(benchmark): benchmark(examples.superdense_coding.main) From 7a1d2c65d4e05bfd90517e816e3cb19489e21835 Mon Sep 17 00:00:00 2001 From: David Cox Date: Tue, 19 Mar 2019 15:00:12 -0400 Subject: [PATCH 3/7] Add Quantum Teleportation to examples_test --- examples/examples_test.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/examples_test.py b/examples/examples_test.py index 8c844806773..690c4baa09c 100644 --- a/examples/examples_test.py +++ b/examples/examples_test.py @@ -9,6 +9,7 @@ import examples.bcs_mean_field import examples.phase_estimator import examples.basic_arithmetic +import examples.quantum_teleportation import examples.superdense_coding @@ -58,6 +59,8 @@ def test_example_runs_basic_arithmetic(): def test_example_runs_phase_estimator(): examples.phase_estimator.main(qnums=(2,), repetitions=2) +def test_example_runs_quantum_teleportation(): + examples.quantum_teleportation.main() def test_example_runs_superdense_coding(): examples.superdense_coding.main() From cb60a154d034b49df2079511edf6baa79e46049b Mon Sep 17 00:00:00 2001 From: David Cox Date: Tue, 19 Mar 2019 16:52:33 -0400 Subject: [PATCH 4/7] Update quantum_teleportation.py --- examples/quantum_teleportation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/quantum_teleportation.py b/examples/quantum_teleportation.py index b203c1c7475..d8cbc23bab6 100644 --- a/examples/quantum_teleportation.py +++ b/examples/quantum_teleportation.py @@ -65,7 +65,7 @@ def main(): # Records in a list each state of q0 for the simulation step_results = sim.simulate_moment_steps(circuit) for step in step_results: - state.append(cirq.bloch_vector_from_state_vector(step.state(), 0)) + state.append(cirq.bloch_vector_from_state_vector(step.state_vector(), 0)) print("\nBloch Sphere of Qubit 0 After Random X and Y Gates:") # Prints the Bloch Sphere of q0 after the X and Y gates From f47a81f5a3140b0b1f9087343eed620edba9e620 Mon Sep 17 00:00:00 2001 From: David Cox Date: Tue, 19 Mar 2019 17:05:41 -0400 Subject: [PATCH 5/7] Update to address Travis CI tests --- examples/quantum_teleportation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/quantum_teleportation.py b/examples/quantum_teleportation.py index d8cbc23bab6..72d42a94f99 100644 --- a/examples/quantum_teleportation.py +++ b/examples/quantum_teleportation.py @@ -65,7 +65,8 @@ def main(): # Records in a list each state of q0 for the simulation step_results = sim.simulate_moment_steps(circuit) for step in step_results: - state.append(cirq.bloch_vector_from_state_vector(step.state_vector(), 0)) + state.append \ + (cirq.bloch_vector_from_state_vector(step.state_vector(), 0)) print("\nBloch Sphere of Qubit 0 After Random X and Y Gates:") # Prints the Bloch Sphere of q0 after the X and Y gates From 835245bdae2113ef44b6e190944265e329a16f96 Mon Sep 17 00:00:00 2001 From: David Cox Date: Wed, 20 Mar 2019 14:08:56 -0400 Subject: [PATCH 6/7] Update quantum_teleportation.py for comments Updated the circuit slightly, changed some nomenclature for key variables, changed the manner in which the message is generated and initially read, and revised the descriptive commentary accordingly. --- examples/quantum_teleportation.py | 82 ++++++++++++++++--------------- 1 file changed, 43 insertions(+), 39 deletions(-) diff --git a/examples/quantum_teleportation.py b/examples/quantum_teleportation.py index 72d42a94f99..9575de16a70 100644 --- a/examples/quantum_teleportation.py +++ b/examples/quantum_teleportation.py @@ -1,15 +1,17 @@ """Quantum Teleportation. Quantum Teleportation is a process by which a quantum state can be transmitted by sending only two classical bits of information. This is accomplished by -pre-sharing an entangled state between the sender and the receiver. This -entangled state allows the receiver of the two classical bits of information -to possess a qubit with the same state as the one held by the sender. - -In the following example output, qubit 0 is set to a random state by applying -X and Y gates. By sending two classical bits of information after qubit 0 and -qubit 1 are measured, the final state of qubit 2 will be identical to the -original random state of qubit 0. This is only possible given that an -entangled state is pre-shared between the sender and receiver. +pre-sharing an entangled state between the sender (Alice) and the receiver +(Bob). This entangled state allows the receiver (Bob) of the two classical +bits of information to possess a qubit with the same state as the one held by +the sender (Alice). + +In the following example output, qubit 0 (the Message) is set to a random state +by applying X and Y gates. By sending two classical bits of information after +qubit 0 (the Message) and qubit 1 (Alice's entangled qubit) are measured, the +final state of qubit 2 (Bob's entangled qubit) will be identical to the +original random state of qubit 0 (the Message). This is only possible given +that an entangled state is pre-shared between Alice and Bob. === REFERENCE === https://en.wikipedia.org/wiki/Quantum_teleportation @@ -17,13 +19,13 @@ === EXAMPLE OUTPUT === Circuit: -0: ---X^0.25---Y^0.125-----------@---H---M-------@--- - | | -1: ----------------------H---@---X-------M---@---|--- - | | | -2: --------------------------X---------------X---@--- +0: -----------X^0.25---Y^0.125---@---H---M-------@--- + | | | +1: ---H---@----------------------X-------M---@---|--- + | | | +2: -------X----------------------------------X---@--- -Bloch Sphere of Qubit 0 After Random X and Y Gates: +Bloch Sphere of Message After Random X and Y Gates: x: 0.2706 y: -0.7071 z: 0.6533 Bloch Sphere of Qubit 2 at Final State: @@ -36,41 +38,43 @@ import cirq -def make_quantum_teleportation_circuit(): +def make_quantum_teleportation_circuit(ranX, ranY): circuit = cirq.Circuit() - q0, q1, q2 = cirq.LineQubit.range(3) - - # Creates a random state for q0 - circuit.append([cirq.X(q0)**random.random(), cirq.Y(q0)**random.random()]) - # Creates Bell State to be shared on q1 and q2 - circuit.append([cirq.H(q1), cirq.CNOT(q1, q2)]) - # Bell measurement of q0 (qubit to be teleported) and q1 (entangled qubit) - circuit.append([cirq.CNOT(q0, q1), cirq.H(q0)]) - circuit.append([cirq.measure(q0), cirq.measure(q1)]) - # Uses q0 and q1 to perform operation on q2 to recover the state of q0 - circuit.append([cirq.CNOT(q1, q2), cirq.CZ(q0, q2)]) + msg, alice, bob = cirq.LineQubit.range(3) + + # Creates Bell state to be shared between Alice and Bob + circuit.append([cirq.H(alice), cirq.CNOT(alice, bob)]) + # Creates a random state for the Message + circuit.append([cirq.X(msg)**ranX, cirq.Y(msg)**ranY]) + # Bell measurement of the Message and Alice's entangled qubit + circuit.append([cirq.CNOT(msg, alice), cirq.H(msg)]) + circuit.append(cirq.measure(msg, alice)) + # Uses the two classical bits from the Bell measurement to recover the + # original quantum Message on Bob's entangled qubit + circuit.append([cirq.CNOT(alice, bob), cirq.CZ(msg, bob)]) return circuit def main(): state = [] - circuit = make_quantum_teleportation_circuit() + ranX = random.random() + ranY = random.random() + circuit = make_quantum_teleportation_circuit(ranX, ranY) print("Circuit:") print(circuit) sim = cirq.Simulator() - # Records in a list each state of q0 for the simulation - step_results = sim.simulate_moment_steps(circuit) - for step in step_results: - state.append \ - (cirq.bloch_vector_from_state_vector(step.state_vector(), 0)) + # Produces the message using random X and Y gates + message = sim.simulate(cirq.Circuit.from_ops( + [cirq.X(0)**ranX, cirq.Y(0)**ranY])) - print("\nBloch Sphere of Qubit 0 After Random X and Y Gates:") - # Prints the Bloch Sphere of q0 after the X and Y gates - b0X, b0Y, b0Z = state[1] + print("\nBloch Sphere of Message After Random X and Y Gates:") + # Prints the Bloch Sphere of the Message after the X and Y gates + b0X, b0Y, b0Z = cirq.bloch_vector_from_state_vector( + message.final_state, 0) print("x: ", np.around(b0X, 4), "y: ", np.around(b0Y, 4), "z: ", np.around(b0Z, 4)) @@ -79,9 +83,9 @@ def main(): final_results = sim.simulate(circuit) print("\nBloch Sphere of Qubit 2 at Final State:") - # Prints the Bloch Sphere of q2 at the final state - b2X, b2Y, b2Z = \ - cirq.bloch_vector_from_state_vector(final_results.final_state, 2) + # Prints the Bloch Sphere of Bob's entangled qubit at the final state + b2X, b2Y, b2Z = cirq.bloch_vector_from_state_vector( + final_results.final_state, 2) print("x: ", np.around(b2X, 4), "y: ", np.around(b2Y, 4), "z: ", np.around(b2Z, 4)) From 89b22db2118d9c2b05f1aed2cc581fda78e38adf Mon Sep 17 00:00:00 2001 From: David Cox Date: Wed, 20 Mar 2019 14:17:31 -0400 Subject: [PATCH 7/7] Update quantum_teleportation.py to address pylint Removed a stray variable that was no longer used. --- examples/quantum_teleportation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/quantum_teleportation.py b/examples/quantum_teleportation.py index 9575de16a70..6dd837b8510 100644 --- a/examples/quantum_teleportation.py +++ b/examples/quantum_teleportation.py @@ -57,7 +57,6 @@ def make_quantum_teleportation_circuit(ranX, ranY): def main(): - state = [] ranX = random.random() ranY = random.random() circuit = make_quantum_teleportation_circuit(ranX, ranY)