{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Noisy Circuits\n",
"\n",
"\n",
"Author: Alexandra Semposki\n",
"\n",
"Maintainer: Alexandra Semposki"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that VQE has been extensively covered for a noiseless system, we can begin to consider what happens when we add noise into the circuit. Of course, some questions one would have are how the noise occurs in the circuit, how does it affect the circuit results, and how would one correct such errors? In this notebook, we'll cover all of these questions and end with an error mitigation technique that will become very useful for the calculations in the rest of this Jupyter Book."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## On the origin of noise\n",
"\n",
"If you've only ever run a quantum circuit on a simulator, you've probably never encountered errors on your qubits (unless you were using a noisy backend, of course). However, if you run your circuit(s) on a real quantum device, like IBM's quantum computers, you'll notice (once your job stops running) that you don't have just the results you thought you would---instead, you'll have some probabilities in bins you didn't expect at all. This is an example of this noise we're talking about. There are two clear places errors can occur that you'll probably be able to guess quite quickly:\n",
"\n",
"- Qubits\n",
"\n",
"- Quantum gates\n",
"\n",
"Both of these hardware components are highly susceptible to error. Some of the general types of error are described below. \n",
"\n",
"- __Decoherence__: a qubit will eventually interact with its environment in a real quantum device, which leads to the qubit entangling with this environment, which in turn leads to the qubit altering from its original state into this entangled state. \n",
"\n",
"\n",
"- __Dephasing__: this term describes the process of a qubit's phase changing over time; this can be visualised by thinking of a qubit as a vector in the Bloch sphere, and then rotating this vector slowly away from its original position. \n",
"\n",
"\n",
"- __Relaxation__: this occurs when a qubit is in a more energetic state, or \"excited\" state, which we can call $|1\\rangle$, and over time it slowly returns back to its \"ground\" state, which we term $|0\\rangle$.\n",
"\n",
"\n",
"We also can describe some of the specific errors that can occur in a qubit, and which lead to the terms described above. \n",
"\n",
"- __Bit flips__: a qubit flips from a 0 to a 1 or vice versa; for example, a state may go from $|000\\rangle$ to $|010\\rangle$ without the action of the quantum gate that would have done this normally. \n",
"\n",
"\n",
"- __Phase flips__: a qubit in a superposition state will experience a sign flip between the two components; for example, for a multi-qubit state, the state may go from $(|001\\rangle + |110\\rangle)$ to $(|001\\rangle - |110\\rangle)$. Again, this is not a command from the user, it is one that appears to spontaneously occur with no command to do so.\n",
"\n",
"\n",
"The two error sources above can be corrected for using more qubits to track which qubit's bit or phase flipped---this is called __error correction__. The two types of error control are __error correction__ and __error mitigation__. Error mitigation is not exactly the same thing as error correction; it deals with current practical methods in reducing error in quantum circuits, but not completely removing these errors \\[1\\]. Error correction tries to do the latter, but often requires hundreds of qubits to perform this task well, which is just not practical at this time for large-scale use. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Noiseless vs. noisy circuits: a simple example\n",
"\n",
"Now let's get our hands dirty and build a very simple circuit to demonstrate this noise. We'll use a noiseless simulator first, and then move to a noisy backend to show the difference between simulated quantum circuits and what would happen on a real quantum device. Let's build a couple of the Bell states to start."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#import qiskit and other needed packages\n",
"import qiskit\n",
"import qiskit.test.mock\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The first Bell state we want to build is given as\n",
"\n",
"$$\n",
"\\frac{|00\\rangle + |11\\rangle}{\\sqrt{2}}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ┌───┐ ░ ┌─┐ \n",
" q_0: ┤ H ├──■───░─┤M├───\n",
" └───┘┌─┴─┐ ░ └╥┘┌─┐\n",
" q_1: ─────┤ X ├─░──╫─┤M├\n",
" └───┘ ░ ║ └╥┘\n",
"meas: 2/══════════════╩══╩═\n",
" 0 1 \n"
]
}
],
"source": [
"#build the first Bell state\n",
"bell1 = qiskit.QuantumCircuit(2)\n",
"bell1.h(0)\n",
"bell1.cx(0,1)\n",
"bell1.measure_all()\n",
"print(bell1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The second Bell state we will build is\n",
"\n",
"$$\n",
"\\frac{|10\\rangle + |01\\rangle}{\\sqrt{2}}\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" ┌───┐ ░ ┌─┐ \n",
" q_0: ┤ H ├──■───░─┤M├───\n",
" ├───┤┌─┴─┐ ░ └╥┘┌─┐\n",
" q_1: ┤ X ├┤ X ├─░──╫─┤M├\n",
" └───┘└───┘ ░ ║ └╥┘\n",
"meas: 2/══════════════╩══╩═\n",
" 0 1 \n"
]
}
],
"source": [
"#second Bell state\n",
"bell2 = qiskit.QuantumCircuit(2)\n",
"bell2.x(1)\n",
"bell2.h(0)\n",
"bell2.cx(0,1)\n",
"bell2.measure_all()\n",
"print(bell2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Noiseless measurement\n",
"\n",
"Now let's do a simple measurement of these circuits. The measurement tool is already included in both circuits, so all we need to do is send these circuits to a noiseless simulator to begin."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counts: {'11': 4104, '00': 4088}\n"
]
}
],
"source": [
"#call the Aer noiseless simulator\n",
"shots = 2**13\n",
"sim = qiskit.Aer.get_backend('aer_simulator')\n",
"qc_trans = qiskit.transpile(bell1, sim)\n",
"counts = sim.run(qc_trans, shots=shots).result().get_counts()\n",
"print('Counts:', counts)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As this is a __very__ simple measurement, and we do not even wish to get any probabilities here, only the number of counts for each state of the Bell state, we only see these counts above. However, it is obvious that there are only two states, $|00\\rangle$ and $|11\\rangle$, the ones we expected. Now let's check the same thing for the second Bell state."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Counts: {'10': 4176, '01': 4016}\n"
]
}
],
"source": [
"#call the Aer noiseless simulator\n",
"qc_trans2 = qiskit.transpile(bell2, sim)\n",
"counts2 = sim.run(qc_trans2, shots=shots).result().get_counts()\n",
"print('Counts:', counts2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As expected, only the $|10\\rangle$ and $|01\\rangle$ state have shown up. Now let's move on to the noisy measurement with a noisy backend and see what changes!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Noisy measurement\n",
"\n",
"We employ the `qiskit.test.mock` module, which uses data from a real IBM quantum device to construct a noisy simulator that will emulate this real quantum computer \\[2\\]. "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Basis gates: ['id', 'u1', 'u2', 'u3', 'cx']\n",
"Number of qubits: 5\n"
]
}
],
"source": [
"#load qiskit noisy backend (with more than 1 qubit)\n",
"noisy_backend = qiskit.test.mock.FakeLondon()\n",
"print(\"Basis gates:\", noisy_backend.configuration().basis_gates)\n",
"print(\"Number of qubits:\", noisy_backend.configuration().num_qubits)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's run our circuits again, on this noisy simulator, and see what we get."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Noisy counts for (|00> + |11>): {'11': 3517, '00': 3960, '01': 396, '10': 319}\n",
"Noisy counts for (|10> + |01>): {'10': 3670, '11': 145, '01': 3794, '00': 583}\n"
]
}
],
"source": [
"#first Bell state\n",
"qc_transn1 = qiskit.transpile(bell1, noisy_backend)\n",
"countsn1 = noisy_backend.run(qc_transn1, shots=shots).result().get_counts()\n",
"print('Noisy counts for (|00> + |11>):', countsn1)\n",
"\n",
"#second Bell state\n",
"qc_transn2 = qiskit.transpile(bell2, noisy_backend)\n",
"countsn2 = noisy_backend.run(qc_transn2, shots=shots).result().get_counts()\n",
"print('Noisy counts for (|10> + |01>):', countsn2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Aha! The counts from the noisy backend clearly show counts in states that are not supposed to be included in the Bell states they correspond to, demonstrating the effect of the noise in a real quantum circuit. This will definitely become an issue with longer and larger computations, and will be something we would like to mitigate. On the next page, we discuss zero-noise extrapolation, a method of error mitigation that we'll be using in future chapters to dispose of as much error as we can while running on real quantum devices."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"- \\[1\\] `mitiq` documentation, \"About Error Mitigation,\" 2020. (Page found [here](https://mitiq.readthedocs.io/en/v.0.1a2/guide/guide_06-error-mitigation.html)).\n",
"- \\[2\\] FRIB-TA Summer School: Quantum Computing and Nuclear Few- and Many-Body Problems, Lecture 11 (link [here](https://github.com/NuclearPhysicsWorkshops/FRIB-TASummerSchoolQuantumComputing/blob/main/doc/pub/lecture11/ipynb/lecture11.ipynb))."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}