Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hello world in Qiskit
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
from math import sqrt
psi = [1/sqrt(2), 1/sqrt(2)]
def prepare(qc, psi):
qc.initialize(psi, [0]) # [0] indicates the zeroth index (or) the first qubit
# State preparation
###########################
qr = QuantumRegister(1)
cr = ClassicalRegister(1)
qc = QuantumCircuit(qr, cr)
prepare(qc, psi)
qc.measure(qr[0], cr[0])
# Measurement
###########################
from qiskit import Aer, transpile
backend = Aer.get_backend('qasm_simulator')
qc_compiled = transpile(qc, backend)
job = backend.run(qc_compiled, shots=1024)
results = job.result()
counts = results.get_counts(qc_compiled)
# Visualization
###########################
from qiskit.visualization import plot_histogram
plot_histogram(counts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment