Skip to content

Instantly share code, notes, and snippets.

@paniash
Last active August 2, 2022 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paniash/52497bf574ea4570ce5f0a21fa093b12 to your computer and use it in GitHub Desktop.
Save paniash/52497bf574ea4570ce5f0a21fa093b12 to your computer and use it in GitHub Desktop.
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