How to Use Qiskit to Run Your First Quantum Algorithm
Getting Started with Qiskit
Qiskit is an open-source Python framework developed by IBM for working with quantum computers. It allows you to design, simulate, and run quantum algorithms on IBM Quantum devices.
Step 1: Install Qiskit
Make sure Python is installed, then use pip to install Qiskit:
pip install qiskit
Verify installation:
import qiskit
print(qiskit.__qiskit_version__)
Step 2: Import Necessary Modules
from qiskit import QuantumCircuit, Aer, execute
QuantumCircuit: Used to create quantum circuits.
Aer: Simulator backend to run circuits locally.
execute: Function to run the quantum circuit.
Step 3: Create Your First Quantum Circuit
We’ll create a simple 1-qubit circuit and apply a Hadamard gate, which puts the qubit into superposition.
# Create a quantum circuit with 1 qubit and 1 classical bit
qc = QuantumCircuit(1, 1)
# Apply Hadamard gate to qubit 0
qc.h(0)
# Measure the qubit into the classical bit
qc.measure(0, 0)
# Draw the circuit
qc.draw(output='mpl')
Hadamard gate (H): Creates superposition, giving qubit a 50/50 chance of being 0 or 1.
Measurement: Collapses the qubit into a definite state.
Step 4: Run the Circuit on a Simulator
# Use the qasm simulator
simulator = Aer.get_backend('qasm_simulator')
# Execute the circuit with 1024 shots (repetitions)
job = execute(qc, simulator, shots=1024)
# Get results
result = job.result()
counts = result.get_counts(qc)
print(counts)
Shots: Number of times the experiment is repeated to gather statistics.
Counts: Shows how often each outcome (0 or 1) occurred.
Step 5: Visualize the Result
from qiskit.visualization import plot_histogram
plot_histogram(counts)
This shows a histogram of 0s and 1s, demonstrating quantum randomness.
Step 6: Optional – Run on a Real Quantum Device
Create an account on IBM Quantum Experience: https://quantum-computing.ibm.com/
Get your API token and save it in Qiskit:
from qiskit import IBMQ
IBMQ.save_account('YOUR_API_TOKEN')
IBMQ.load_account()
Select a real backend and run your circuit:
provider = IBMQ.get_provider(hub='ibm-q')
backend = provider.get_backend('ibmq_quito') # Example real quantum device
job = execute(qc, backend=backend, shots=1024)
result = job.result()
counts = result.get_counts(qc)
print(counts)
✅ Summary
Install Qiskit and import modules.
Create a quantum circuit with qubits and classical bits.
Apply gates (like Hadamard) and measure.
Simulate locally using Aer.
Optionally, run on IBM Quantum devices.
Visualize results with histograms.
This simple exercise introduces you to superposition, measurement, and quantum randomness—the building blocks of quantum computing.
Learn Quantum Computing Course in Hyderabad
Read More
Quantum Computing Projects for College Students
How to Build Your First Quantum Circuit Step-by-Step
Top Beginner Quantum Computing Projects to Try
Comments
Post a Comment