How To Create Quantum Circuits With Qiskit
Solution to Question 1 and 2 of the ongoing Quantum programming challenge
Hey everyone!
Last week, I published a challenge with 6 Qiskit questions to get started with Quantum Computing.
Here are the solutions to the first two questions from the challenge.
Question 1
Create a 1-qubit quantum circuit that:
Starts in the state |0>
Applies a Hadamard gate
Measures the qubit
Write the Qiskit code and simulate the circuit using a Qiskit simulator.
Solution 1
We start by installing Qiskit and the Qiskit Aer library.
Qiskit Aer is a high-performance library with realistic noise models to run quantum computing simulations on classical computers.
!uv pip install qiskit qiskit-aer If you’re interested in running the following code on a real quantum computer, refer to the following article.
Next, we import the necessary packages.
from qiskit import QuantumCircuit # To create and manipulate quantum circuits
from qiskit_aer import Aer # Qiskit simulator backend
from qiskit.visualization import plot_histogram # To visualise measurement results We then create the quantum circuit using QuantumCircuit(1, 1) where the:
First
1represents one qubit. This qubit is in state |0>.Second
1represents one classical bit used to store the measurement result.
# Create circuit
circuit = QuantumCircuit(1, 1) Next, we apply the Hadamard gate to the qubit and then measure it.
# Apply Hadamard gate to qubit at index 0
circuit.h(0)
# Measure qubit at index 0 and store in classical bit at index 0
circuit.measure(0, 0) 


