Skip to content

Instantly share code, notes, and snippets.

@willzeng
willzeng / qc_intro.py
Created November 5, 2019 17:35
A quick introduction to quantum states and unitary operations
# the state of N qubits is a complex vector of dimension 2^N
n_qubits = 2
dimension = 2 ** n_qubits
state = [1j, 0, 0, 0]
def print_probs(state, n_qubits):
# the elements of this state are squared to calculate outcome probabilities
for bitstring in range(n_qubits ** 2):
probability = np.abs(state[bitstring]) ** 2
from pyquil.quil import Program
from pyquil.api import QPUConnection
from pyquil.gates import H, CNOT
qpu = QPUConnection(device_name='19Q-Acorn')
bell_state = Program(H(0), CNOT(0, 1))
result = qpu.run_and_measure(bell_state, [0, 1])