-
-
Notifications
You must be signed in to change notification settings - Fork 46k
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
Create q_full_adder.py #6735
Merged
Merged
Create q_full_adder.py #6735
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
61a5708
Create q_full_adder.py
KevinJoven11 6527950
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 18102cb
Erase the unused numpy library
KevinJoven11 2bd9282
Create the doctest.
KevinJoven11 c3204d4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 0b715a5
doctest for negative numbers, float, etc.
KevinJoven11 ab40bc9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
""" | ||
Build the quantum full adder (QFA) for any sum of | ||
two quantum registers and one carry in. This circuit | ||
is designed using the Qiskit framework. This | ||
experiment run in IBM Q simulator with 1000 shots. | ||
. | ||
References: | ||
https://www.quantum-inspire.com/kbase/full-adder/ | ||
""" | ||
|
||
import math | ||
|
||
import qiskit | ||
from qiskit import Aer, ClassicalRegister, QuantumCircuit, QuantumRegister, execute | ||
|
||
|
||
def quantum_full_adder( | ||
input_1: int = 1, input_2: int = 1, carry_in: int = 1 | ||
) -> qiskit.result.counts.Counts: | ||
""" | ||
# >>> q_full_adder(inp_1, inp_2, cin) | ||
# the inputs can be 0/1 for qubits in define | ||
# values, or can be in a superposition of both | ||
# states with hadamard gate using the input value 2. | ||
# result for default values: {11: 1000} | ||
qr_0: ──■────■──────────────■── | ||
│ ┌─┴─┐ ┌─┴─┐ | ||
qr_1: ──■──┤ X ├──■────■──┤ X ├ | ||
│ └───┘ │ ┌─┴─┐└───┘ | ||
qr_2: ──┼─────────■──┤ X ├───── | ||
┌─┴─┐ ┌─┴─┐└───┘ | ||
qr_3: ┤ X ├─────┤ X ├────────── | ||
└───┘ └───┘ | ||
cr: 2/═════════════════════════ | ||
Args: | ||
input_1: input 1 for the circuit. | ||
input_2: input 2 for the circuit. | ||
carry_in: carry in for the circuit. | ||
Returns: | ||
qiskit.result.counts.Counts: sum result counts. | ||
>>> quantum_full_adder(1,1,1) | ||
{'11': 1000} | ||
>>> quantum_full_adder(0,0,1) | ||
{'01': 1000} | ||
>>> quantum_full_adder(1,0,1) | ||
{'10': 1000} | ||
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. Please add some tests that fail... Negative numbers, floating point numbers, letters, etc. 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. Done! |
||
>>> quantum_full_adder(1,-4,1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: inputs must be positive. | ||
>>> quantum_full_adder('q',0,1) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: inputs must be integers. | ||
>>> quantum_full_adder(0.5,0,1) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: inputs must be exact integers. | ||
>>> quantum_full_adder(0,1,3) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: inputs must be less or equal to 2. | ||
""" | ||
if (type(input_1) == str) or (type(input_2) == str) or (type(carry_in) == str): | ||
raise TypeError("inputs must be integers.") | ||
|
||
if (input_1 < 0) or (input_2 < 0) or (carry_in < 0): | ||
raise ValueError("inputs must be positive.") | ||
|
||
if ( | ||
(math.floor(input_1) != input_1) | ||
or (math.floor(input_2) != input_2) | ||
or (math.floor(carry_in) != carry_in) | ||
): | ||
raise ValueError("inputs must be exact integers.") | ||
|
||
if (input_1 > 2) or (input_2 > 2) or (carry_in > 2): | ||
raise ValueError("inputs must be less or equal to 2.") | ||
|
||
# build registers | ||
qr = QuantumRegister(4, "qr") | ||
cr = ClassicalRegister(2, "cr") | ||
# list the entries | ||
entry = [input_1, input_2, carry_in] | ||
|
||
quantum_circuit = QuantumCircuit(qr, cr) | ||
|
||
for i in range(0, 3): | ||
if entry[i] == 2: | ||
quantum_circuit.h(i) # for hadamard entries | ||
elif entry[i] == 1: | ||
quantum_circuit.x(i) # for 1 entries | ||
elif entry[i] == 0: | ||
quantum_circuit.i(i) # for 0 entries | ||
|
||
# build the circuit | ||
quantum_circuit.ccx(0, 1, 3) # ccx = toffoli gate | ||
quantum_circuit.cx(0, 1) | ||
quantum_circuit.ccx(1, 2, 3) | ||
quantum_circuit.cx(1, 2) | ||
quantum_circuit.cx(0, 1) | ||
|
||
quantum_circuit.measure([2, 3], cr) # measure the last two qbits | ||
|
||
backend = Aer.get_backend("qasm_simulator") | ||
job = execute(quantum_circuit, backend, shots=1000) | ||
|
||
return job.result().get_counts(quantum_circuit) | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"Total sum count for state is: {quantum_full_adder(1,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.
Kinda cool how pre-commit fixes all this stuff… trailing whitespace, unsorted imports, single quotes, etc.