Quantum Computing : Building Your First Quantum Circuit

a simple quantum circuit that prepares the state |0⟩ and applies a Hadamard gate to it, resulting in the superposition state (|0⟩ + |1⟩)/√2:

from qiskit import QuantumCircuit, Aer, execute

# create a quantum circuit with one qubit
qc = QuantumCircuit(1, 1)

# apply a Hadamard gate to the qubit
qc.h(0)

# measure the qubit and store the result in a classical bit
qc.measure(0, 0)

# execute the circuit on a simulator backend
backend = Aer.get_backend(\'qasm_simulator\')
job = execute(qc, backend)
result = job.result()

# print the measurement outcome
counts = result.get_counts(qc)
print(counts)

In this circuit, QuantumCircuit(1, 1) creates a quantum circuit with one qubit and one classical bit. The h(0) method applies a Hadamard gate to the first qubit (indexed as 0), and measure(0, 0) measures the qubit and stores the result in the first classical bit (indexed as 0). Finally, execute(qc, backend) runs the circuit on a simulator backend (qasm_simulator), and result.get_counts(qc) returns the measurement outcome as a dictionary of counts (e.g., {\’0\’: 1} or {\’0\’: 1, \’1\’: 3}).

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *