Created
February 4, 2020 06:19
-
-
Save GDLMadushanka/12c63da1a218bfc3cca87f13ed99fa9f to your computer and use it in GitHub Desktop.
Half-Adder implementation using qiskit
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
from qiskit import * | |
from qiskit.tools.visualization import plot_bloch_multivector | |
from qiskit.tools.visualization import plot_histogram | |
# Creating a circuit with 3 quantum bits and 2 classical bits | |
qc = QuantumCircuit(3,2) | |
# Preparing inputs | |
qc.x(0) # Comment this line to make Qbit0 = |0> | |
qc.x(1) # Comment this line to make Qbit1 = |0> | |
# no changes to Qbit2 (stays |0> always) | |
qc.barrier() | |
# Applying AND operation and put result to Qbit2 | |
qc.ccx(0,1,2) | |
qc.barrier() | |
# Applying XOR operation and put result to Qbit1 | |
qc.cx(0,1) | |
qc.barrier() | |
# Reading outputs | |
qc.measure(1,0) # Reading XOR value ( sum bit ) | |
qc.measure(2,1) # Reading AND value ( carry-out bit ) | |
qc.draw(output='mpl') | |
# Run the experimient 1024 times and get stats | |
counts = execute(qc,Aer.get_backend('qasm_simulator')).result().get_counts() | |
print(counts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment