# Qiskit Textbook: Chapter 1 Exercises

Published:

All content below has been taken from Chapter 1 of the Qiskit Textbook. I have only extracted bits of it to emphasize the exercises and rephrase some parts in my own words.

## Installation

The Qiskit Textbook provides some tools and widgets for educational purposes. These are not part of Qiskit itself and are available through the qiskit_textbook package.

Install the package using Pip and Git using the terminal command:

pip3 install git+https://github.com/qiskit-community/qiskit-textbook.git#subdirectory=qiskit-textbook-src


To reproduce the Matplotlib visualizations shown in the Qiskit Textbook create a file at

~/.qiskit/settings.conf


with contents:

[default]
circuit_drawer = mpl


To set the default image format to SVG, add

c.InlineBackend.figure_format = 'svg'


to the file:

~/.ipython/profile_default/ipython_kernel_config.py


We can use JupyterLab for these exercises. Install it and some other packages that we’ll need using pip:

pip3 install numexpr pylatexenc jupyterlab


Now you can open a lab page in your browser simply using the terminal command:

jupyter lab


To check everything is working ok, try to run the following code block in the notebook:

from qiskit import QuantumCircuit
from qiskit_textbook.widgets import binary_widget


If that runs without errors, we should be good to go. You can delete that code block or start a new notebook, as you prefer.

## The Atoms of Computation

### Classical bits

Run the following piece of code in a Python cell in your notebook.

from qiskit_textbook.widgets import binary_widget
binary_widget(nbits=5)


It should open a little widget to experiment with binary numbers:

1. Think of a number between 0 and 31 and try to write it down on paper in binary, then check it in the widget.
2. If your system can be fully described by $n$ bits, how many different states can it be in?

### Building a simple quantum circuit

To build up a little muscle memory for using Qiskit, let’s try to build a simple circuit from the Qiskit Textbook without directly consulting it using the following steps:

1. Import QuantumCircuit, Aer, and assemble from the qiskit package.
2. Import the plot_histogram function from the qiskit.visualization package.
3. Create a quantum circuit with $4$ qubits and $2$ classical bits (hint: see QuantumCircuit).
4. Use a quantum gate to put $q_0$ in the state $\vert 1\rangle$ (note: qubits are initialized in state $\vert 0\rangle$).
5. Use a quantum gate to put $q_1$ in the state $\vert 1\rangle$.
6. Add a barrier to visually separate the input from the processing part of the circuit.
7. Add a CX gate with $q_0$ as the control and $q_2$ as the target.
8. Add a CX gate with $q_1$ as the control and $q_2$ as the target.
9. Add a CCX gate with $q_0$ and $q_1$ as controls and $q_3$ as the target.
10. Add another barrier to separate the processing part of the circuit from the output part.
11. Measure $q_2$ and write the output to the classical bit $c_0$.
12. Measure $q_3$ and write the output to the classical bit $c_1$.

We’ve now designed a quantum circuit, which is a list of instructions bound to some registers. To actually execute it we need to assemble it into instructions that can be understood by a quantum computer. In a new Python cell:

1. Assemble the circuit into a qobj using the assemble function we imported above.

This object is written in Open Quantum Assembly Language (OpenQASM). Now we need to submit this assembled code to an assember:

1. Create a simulator with the 'qasm_simulator' backend using the Aer backend provider.
2. Run the qobj on the simulator and extract the result.
3. Get the counts from the result.
4. Plot the counts histogram using the function we imported above with counts as input.

You should find the result $10$. I.e., $01 + 01 = 10$ in binary, or $1+1=2$ in decimal:

1. As a sanity check, try changing the input gates to find $00 + 00$, $00 + 01$, and $01 + 00$.
2. Try adding other gates (e.g., the H-gate) and seeing how this changes the output.

## Representing Qubit States

### State vectors

Answer the following questions on paper. Given a general single-qubit state vector of the form:

$\vert q \rangle = \alpha \vert 0 \rangle + \beta \vert 1 \rangle,$

where,

$\alpha, \beta \in \mathbb{C},$
1. Find values of $\alpha$ and $\beta$ that will give a $1/3$ probability of measuring $0$.
2. Create a different state vector that will give the same measurement probabilities.
3. Verify that the probability of measuring $1$ given these two states is $2/3$.

You can check your answers using a widget by running the following code snippet in a notebook:

from qiskit_textbook.widgets import state_vector_exercise
state_vector_exercise(target=1/3)


### The Bloch sphere

To plot any single-qubit state on the surface of a sphere, known as the Bloch sphere, we re-parameterize as,

$\vert q \rangle = \cos \tfrac{\theta}{2}\vert 0 \rangle + e^{i\phi}\sin\tfrac{\theta}{2}\vert 1 \rangle$

where,

$\theta, \phi \in \mathbb{R}.$

The following code snippet will allow you to plot the Bloch sphere using spherical polar coordinates as input.

from math import pi
from qiskit_textbook.widgets import plot_bloch_vector_spherical
theta = pi / 2
phi = 0
plot_bloch_vector_spherical(coords)


Use plot_bloch_vector_spherical() to plot a qubit in the states:

1. $\vert 0\rangle$
2. $\vert 1\rangle$
3. $\tfrac{1}{\sqrt{2}}(\vert 0\rangle + \vert 1\rangle)$
4. $\tfrac{1}{\sqrt{2}}(\vert 0\rangle - i\vert 1\rangle)$
5. $\tfrac{1}{\sqrt{2}}\begin{bmatrix}i\\ 1\end{bmatrix}$

As we move through Chapter 1 of the Qiskit Textbook, I will update this page with more exercises.

Tags: