Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Created October 24, 2020 21:53
Show Gist options
  • Save pedrominicz/65895fc99715f55f170346662e0126b0 to your computer and use it in GitHub Desktop.
Save pedrominicz/65895fc99715f55f170346662e0126b0 to your computer and use it in GitHub Desktop.
Simple Qiskit example.
import numpy as np
from qiskit import *
from qiskit.circuit.library import *
sim = Aer.get_backend('qasm_simulator')
# Create a quantum circuit with 2 qubits and 2 classical bits.
qc = QuantumCircuit(2, 2)
# Add a single-qubit Hadamard gate on qubit 0.
qc.h(0)
# Add a controlled-X (controlled-Not) gate on control qubit 0 and target qubit
# 1.
qc.cx(0, 1)
# Measure qubits 0 and 1 to classical bits 0 and 1 respectively.
qc.measure([0, 1], [0, 1])
# `shots` specified the number of repetitions for each circuit, for sampling.
# The default is `1024`.
job = execute(qc, sim, shots=1000)
result = job.result()
counts = result.get_counts(qc)
print(counts)
# The circuit created forms a Bell state. A Bell state is the simplest example
# of entanglement. The `H` gate puts `q_0` into a superposition then the `X`
# gate (controlled-Not) entangles both qubits. When measured, the qubits will
# collapse to either `0` or `1` and, by the particular way they are entangled,
# `q_1 = q_0 ^ q_1`.
#
# ┌───┐ ┌─┐
# q_0: ┤ H ├──■──┤M├───
# └───┘┌─┴─┐└╥┘┌─┐
# q_1: ─────┤ X ├─╫─┤M├
# └───┘ ║ └╥┘
# c: 2/═══════════╩══╩═
# 0 1
#
print(qc.draw())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment