-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Quantum Teleportation to examples. #1449
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
717f83c
Add Quantum Teleportation to examples.
davemc84 b0eb6da
Add Quantum Teleportation to examples_perf_test.py
davemc84 d2ea3d5
Merge pull request #4 from davemc84/patch-2
davemc84 7a1d2c6
Add Quantum Teleportation to examples_test
davemc84 cb60a15
Update quantum_teleportation.py
davemc84 f47a81f
Update to address Travis CI tests
davemc84 835245b
Update quantum_teleportation.py for comments
davemc84 89b22db
Update quantum_teleportation.py to address pylint
davemc84 8bc291c
Merge branch 'master' into patch-1
davemc84 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
"""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) | ||
davemc84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Creates a random state for q0 | ||
circuit.append([cirq.X(q0)**random.random(), cirq.Y(q0)**random.random()]) | ||
davemc84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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)]) | ||
davemc84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# 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 \ | ||
davemc84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(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 | ||
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 = \ | ||
davemc84 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cirq.Circuit.from_ops
would avoid a lot ofappend
boilerplate.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks very much for this feedback. Please let me know if I misunderstood your comment, but I have amended the code to be as follows:
message = sim.simulate(cirq.Circuit.from_ops([cirq.X(0)**ranX, cirq.Y(0)**ranY]))
which avoids theappend
boilerplate to get the bloch sphere of the message.Again, please do let me know if I misunderstood what you were looking for here and thanks so much for this suggestion!