-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Create superdense_coding.py #7349
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,102 @@ | ||
""" | ||
Build the superdense coding protocol. This quantum | ||
circuit can send two classical bits using one quantum | ||
bit. This circuit is designed using the Qiskit | ||
framework. This experiment run in IBM Q simulator | ||
with 1000 shots. | ||
. | ||
References: | ||
https://qiskit.org/textbook/ch-algorithms/superdense-coding.html | ||
https://en.wikipedia.org/wiki/Superdense_coding | ||
""" | ||
|
||
import math | ||
|
||
import qiskit | ||
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute | ||
|
||
|
||
def superdense_coding(bit_1: int = 1, bit_2: int = 1) -> qiskit.result.counts.Counts: | ||
""" | ||
The input refer to the classical message | ||
that you wants to send. {'00','01','10','11'} | ||
result for default values: {11: 1000} | ||
┌───┐ ┌───┐ | ||
qr_0: ─────┤ X ├──────────┤ X ├───── | ||
┌───┐└─┬─┘┌───┐┌───┐└─┬─┘┌───┐ | ||
qr_1: ┤ H ├──■──┤ X ├┤ Z ├──■──┤ H ├ | ||
└───┘ └───┘└───┘ └───┘ | ||
cr: 2/══════════════════════════════ | ||
Args: | ||
bit_1: bit 1 of classical information to send. | ||
bit_2: bit 2 of classical information to send. | ||
Returns: | ||
qiskit.result.counts.Counts: counts of send state. | ||
>>> superdense_coding(0,0) | ||
{'00': 1000} | ||
>>> superdense_coding(0,1) | ||
{'01': 1000} | ||
>>> superdense_coding(-1,0) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: inputs must be positive. | ||
>>> superdense_coding(1,'j') | ||
Traceback (most recent call last): | ||
... | ||
TypeError: inputs must be integers. | ||
>>> superdense_coding(1,0.5) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: inputs must be exact integers. | ||
>>> superdense_coding(2,1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: inputs must be less or equal to 1. | ||
""" | ||
if (type(bit_1) == str) or (type(bit_2) == str): | ||
raise TypeError("inputs must be integers.") | ||
if (bit_1 < 0) or (bit_2 < 0): | ||
raise ValueError("inputs must be positive.") | ||
if (math.floor(bit_1) != bit_1) or (math.floor(bit_2) != bit_2): | ||
raise ValueError("inputs must be exact integers.") | ||
if (bit_1 > 1) or (bit_2 > 1): | ||
raise ValueError("inputs must be less or equal to 1.") | ||
|
||
# build registers | ||
qr = QuantumRegister(2, "qr") | ||
cr = ClassicalRegister(2, "cr") | ||
|
||
quantum_circuit = QuantumCircuit(qr, cr) | ||
|
||
# entanglement the qubits | ||
quantum_circuit.h(1) | ||
quantum_circuit.cx(1, 0) | ||
|
||
# send the information | ||
c_information = str(bit_1) + str(bit_2) | ||
|
||
if c_information == "11": | ||
quantum_circuit.x(1) | ||
quantum_circuit.z(1) | ||
elif c_information == "10": | ||
quantum_circuit.z(1) | ||
elif c_information == "01": | ||
quantum_circuit.x(1) | ||
else: | ||
quantum_circuit.i(1) | ||
|
||
# unentangled the circuit | ||
quantum_circuit.cx(1, 0) | ||
quantum_circuit.h(1) | ||
|
||
# measure the circuit | ||
quantum_circuit.measure(qr, cr) | ||
|
||
backend = Aer.get_backend("qasm_simulator") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
job = execute(quantum_circuit, backend, shots=1000) | ||
|
||
return job.result().get_counts(quantum_circuit) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"Counts for classical state send: {superdense_coding(1,1)}") |
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.