Skip to content

Instantly share code, notes, and snippets.

@GDLMadushanka
Created February 4, 2020 05:41
Show Gist options
  • Save GDLMadushanka/b15ae6fa51d7aa84a11b620c96fa8c5b to your computer and use it in GitHub Desktop.
Save GDLMadushanka/b15ae6fa51d7aa84a11b620c96fa8c5b to your computer and use it in GitHub Desktop.
AND gate using CCNOT
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 one classical bit
qc = QuantumCircuit(3,1)
# 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 the CCNOT gate
qc.ccx(0,1,2)
qc.barrier()
# Measuring Qbit2 and put result to classical bit
qc.measure(2,0)
qc.draw(output='mpl')
# Run the experimient 1024 times and get stats
counts = execute(qc,Aer.get_backend('qasm_simulator')).result().get_counts()
plot_histogram(counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment