# Import necessary packages
from qiskit import QuantumCircuit, Aer, transpile
from qiskit.primitives import Sampler
from qiskit.visualization import plot_histogram
import matplotlib.pyplot as plt
Qiskit Introductory Homework
Homework Objectives:
1. Install and set up Qiskit.
2. Learn how to create a basic quantum circuit.
3. Run a quantum circuit on a simulator.
Install Qiskit by running the following command in your terminal or within this notebook:
$ !pip install qiskit
If you are using python environments make sure that you are installing to the environment you are using for your notebook.
Main imports: - QuantumCircuit: Used to create and manage quantum circuits. - Aer: Provides access to simulators. - transpile: Optimizes circuits for specific backends. - plot_histogram: Visualizes measurement outcomes.
Create a quantum circuit with 2 qubits and 2 classical bits.
Syntax related to building your first Q circuit: - qc = QuantumCircuit(n, m)
: Creates a quantum circuit object with n quantum bits (qubits) and m classical bits. The classical bits are used to record the measurement results, so generally we will initialize the same number of classical bits as we have qubits. - qc.h(i)
: Applies a Hadamard gate to the i’th qubit. - qc.cx(i, j)
: Adds a CNOT gate with qubit i as control and qubit j as target. - qc.measure([a, b], [c, d])
: Measures qubits a and b and stores the results in classical bits c and d. For larger circuits, the first parameter is a list of indices to measure, and the second parameter is a list of classical indices to store the result.
# We will be creating an entangled Bell pair.
# Create a quantum circuit with 2 qubits. Apply a
# 1. Hadamard gate to the first qubit and then a
# 2. CNOT gate with the first qubit as the control and the second qubit as the target.
= # This will be your circuit
qc # Your code will be written here
Measure the qubits
Measure them in the computational (standard) basis. The result of measurement on the Qubit 0 and 1 should be recorded in the classical bits 0 and 1 respectively.
# Your code will be written here
0, 1], [0, 1]) qc.measure([
We can visualize our circuit by using the print function.
print("Quantum Circuit:")
print(qc)
Use the Aer simulator to execute the quantum circuit.
You may use the qasm_simulator. Then execute the circuit on the simulator.
How to simulate the built circuit: - Aer.get_backend(‘qasm_simulator’): Retrieves the QASM simulator backend. - transpile(qc, simulator): Optimizes the circuit for the selected backend. - simulator.run(): Runs the transpiled circuit on the simulator.
= # Call the simulator
simulator
# Transpile the circuit for the simulator
= transpile(qc, simulator)
transpiled_qc
# Execute the circuit on the qasm simulator
= # Run the transpiled_qc
job
# Get the result of the simulation
# Get the counts (measurement results)
# Print the counts
print("Simulation Results:")
print(counts)
# Use a histogram to visualize the measurement outcomes.
# Plot the histogram
plt.hist(counts) plt.show()
Get the result of the simulation. Then get the counts (measurement results).
You may need to use the functions result() and get_counts()
= # Your code will be here
result = # Your code will be here counts
Display the result.
print("Simulation Results:")
print(counts)
plt.hist(counts) plt.show()