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.

# 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

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.

qc = # This will be your circuit
# 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

qc.measure([0, 1], [0, 1])

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.

simulator = # Call the simulator

# Transpile the circuit for the simulator
transpiled_qc = transpile(qc, simulator)

# Execute the circuit on the qasm simulator
job = # Run the transpiled_qc

# 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()

result = # Your code will be here
counts = # Your code will be here

Display the result.

print("Simulation Results:")
print(counts)

plt.hist(counts)
plt.show()

Now you may be able to answer the following questions:

1. What does the Hadamard gate do to the first qubit in the circuit?

2. What is the state after applying the CNOT gate?

3. What are the measurement probabilities observed from the simulation?

4. Try first applying the CNOT then Hadamard. Does the final state changes?

5. Try modifying the circuit by adding more gates (e.g., X, Y, or Z gates). Can you build the state \(|01> + i|10>\)?