Skip to content

Instantly share code, notes, and snippets.

@GDLMadushanka
Created February 4, 2020 02:13
Show Gist options
  • Save GDLMadushanka/b02f44c072f886cbd20e97565e18db32 to your computer and use it in GitHub Desktop.
Save GDLMadushanka/b02f44c072f886cbd20e97565e18db32 to your computer and use it in GitHub Desktop.
XOR implementation using CNOT gate
from qiskit import *
from qiskit.tools.visualization import plot_bloch_multivector
from qiskit.tools.visualization import plot_histogram
# Creating a circuit with 2 quantum bits and one classical bit
qc = QuantumCircuit(2,1)
# Preparing inputs
qc.x(0) # Comment this line to make Qbit0 = |0>
qc.x(1) # Comment this line to make Qbit1 = |0>
qc.barrier()
# Applying the CNOT gate
qc.cx(0,1)
qc.barrier()
# Measuring Qbit1 and put result to classical bit
qc.measure(1,0)
# Drawing the circuit diagram
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