Last active
August 2, 2022 06:50
-
-
Save paniash/52497bf574ea4570ce5f0a21fa093b12 to your computer and use it in GitHub Desktop.
Hello world in Qiskit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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