{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# The Qubit\n", "\n", "This tutorial introduces you to one of the core concepts in quantum computing - the qubit, and its representation in mathematical notation and in Q# code.\n", "\n", "If you aren't familiar with [complex arithmetic](../ComplexArithmetic/ComplexArithmetic.ipynb) and [linear algebra](../LinearAlgebra/LinearAlgebra.ipynb), we recommend that you complete those tutorials first.\n", "\n", "This tutorial covers the following topics:\n", "* The concept of a qubit\n", "* Superposition\n", "* Vector representation of qubit states\n", "* Dirac notation\n", "* `Qubit` data type in Q#" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The Concept of a Qubit\n", "\n", "The basic building block of a classical computer is the bit - a single memory cell that is either in state $0$ or in state $1$. Similarly, the basic building block of a quantum computer is the quantum bit, or **qubit**. Like the classical bit, a qubit can be in state $0$ or in state $1$. Unlike the classical bit, however, the qubit isn't limited to just those two states - it may also be in a combination, or **superposition** of those states.\n", "\n", "> A common misconception about quantum computing is that a qubit is always in one state or the other, we just don't know which one until we \"measure\" it. That is not the case. A qubit in a superposition is in a state between the states $0$ and $1$. When a qubit is measured, it is forced entirely into one state or the other - in other words, measuring it actually changes its state." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Matrix Representation\n", "\n", "The state of a qubit is represented by a complex vector of size 2:\n", "\n", "$$\\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix}$$\n", "\n", "Here $\\alpha$ represents how \"close\" the qubit is to the state $0$, and $\\beta$ represents how \"close\" the qubit is to the state $1$. This vector is normalized: $|\\alpha|^2 + |\\beta|^2 = 1$.\n", "\n", "$\\alpha$ and $\\beta$ are known as **amplitudes** of states $0$ and $1$, respectively." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Basis States\n", "\n", "A qubit in state $0$ would be represented by the following vector:\n", "\n", "$$\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}$$\n", "\n", "Likewise, a qubit in state $1$ would be represented by this vector:\n", "\n", "$$\\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}$$\n", "\n", "Note that you can use scalar multiplication and vector addition to express any qubit state as a sum of these two vectors with certain weights (known as **linear combination**):\n", "\n", "$$\\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} =\n", "\\begin{bmatrix} \\alpha \\\\ 0 \\end{bmatrix} + \\begin{bmatrix} 0 \\\\ \\beta \\end{bmatrix} =\n", "\\alpha \\cdot \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} + \\beta \\cdot \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}$$\n", "\n", "Because of this, these two states are known as **basis states**.\n", "\n", "These two vectors have two additional properties. First, as mentioned before, both are **normalized**:\n", "\n", "$$\\langle \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} , \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} \\rangle =\n", "\\langle \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} , \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} \\rangle = 1$$\n", "\n", "Second, they are **orthogonal** to each other:\n", "\n", "$$\\langle \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} , \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} \\rangle =\n", "\\langle \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix} , \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix} \\rangle = 0$$\n", "\n", "> As a reminder, $\\langle V , W \\rangle$ is the [inner product](../LinearAlgebra/LinearAlgebra.ipynb#Inner-Product) of $V$ and $W$.\n", "\n", "This means that these vectors form an **orthonormal basis**. The basis of $\\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}$ and $\\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}$ is called the **computational basis**, also known as the **canonical basis**.\n", "\n", "> There exist other orthonormal bases, for example, the **Hadamard basis**, formed by the vectors\n", ">\n", "> $$\\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{2}} \\end{bmatrix} \\text{ and } \\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ -\\frac{1}{\\sqrt{2}} \\end{bmatrix}$$\n", ">\n", "> You can check that these vectors are normalized, and orthogonal to each other. Any qubit state can be expressed as a linear combination of these vectors:\n", ">\n", "> $$\\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} =\n", "\\frac{\\alpha + \\beta}{\\sqrt{2}} \\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{2}} \\end{bmatrix} +\n", "\\frac{\\alpha - \\beta}{\\sqrt{2}} \\begin{bmatrix} \\frac{1}{\\sqrt{2}} \\\\ -\\frac{1}{\\sqrt{2}} \\end{bmatrix}$$\n", ">\n", "> The Hadamard basis is widely used in quantum computing, for example, in the [BB84 quantum key distribution protocol](https://en.wikipedia.org/wiki/BB84)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dirac Notation\n", "\n", "Writing out each vector when doing quantum calculations takes up a lot of space, and this will get even worse once we introduce quantum gates and multi-qubit systems. **Dirac notation** is a shorthand notation that helps solve this issue. In Dirac notation, a vector is denoted by a symbol called a **ket**. For example, a qubit in state $0$ is represented by the ket $|0\\rangle$, and a qubit in state $1$ is represented by the ket $|1\\rangle$:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "
$|0\\rangle = \\begin{bmatrix} 1 \\\\ 0 \\end{bmatrix}$$|1\\rangle = \\begin{bmatrix} 0 \\\\ 1 \\end{bmatrix}$
\n", "\n", "These two kets represent basis states, so they can be used to represent any other state:\n", "\n", "$$\\begin{bmatrix} \\alpha \\\\ \\beta \\end{bmatrix} = \\alpha|0\\rangle + \\beta|1\\rangle$$\n", "\n", "Any symbol other than $0$ or $1$ within the ket can be used to represent arbitrary vectors, similar to how variables are used in algebra: \n", "\n", "$$|\\psi\\rangle = \\alpha|0\\rangle + \\beta|1\\rangle$$\n", "\n", "Several ket symbols have a generally accepted use, such as:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
$|+\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + |1\\rangle\\big)$$|-\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - |1\\rangle\\big)$
$|i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle + i|1\\rangle\\big)$$|-i\\rangle = \\frac{1}{\\sqrt{2}}\\big(|0\\rangle - i|1\\rangle\\big)$
\n", "\n", "We will learn more about Dirac notation in the next tutorials, as we introduce quantum gates and multi-qubit systems." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Q#\n", "===\n", "\n", "## Qubit data type\n", "\n", "In Q#, qubits are represented by the `Qubit` data type. On a physical quantum computer, it's impossible to directly access the state of a qubit, whether to read its exact state, or to set it to a desired state, and this data type reflects that. Instead, you can change the state of a qubit using [quantum gates](../SingleQubitGates/SingleQubitGates.ipynb), and extract information about the state of the system using measurements.\n", "\n", "That being said, when you run Q# code on a quantum simulator instead of a physical quantum computer, you can use diagnostic functions that allow you to peek at the state of the quantum system. This is very useful both for learning and for debugging small Q# programs.\n", "\n", "The qubits aren't an ordinary data type, so the variables of this type have to be declared and initialized (\"allocated\") a little differently:\n", "\n", "```c#\n", "// This statement allocates a qubit, and binds it to the variable q\n", "use q = Qubit();\n", "// You can work with the qubit here\n", "// ...\n", "\n", "// The qubit is deallocated once it's not used any longer\n", "```\n", "\n", "> An alternative syntax for qubit allocation defines the scope in which the allocated qubits are available explicitly:\n", "```c#\n", "// This statement allocates a qubit, and binds it to the variable q\n", "use q = Qubit() {\n", " // You can work with the qubit here\n", " // ...\n", "}\n", "// The qubit is no longer allocated outside of the 'use' block\n", "```\n", "\n", "Freshly allocated qubits start out in state $|0\\rangle$, and have to be returned to that state by the time they are released. If you attempt to release a qubit in any state other than $|0\\rangle$, your program will throw a `ReleasedQubitsAreNotInZeroStateException`. We will see why it is important later, when we look at multi-qubit systems." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Demo: Examining Qubit States in Q#\n", "\n", "We will be using the function [`DumpMachine`](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.diagnostics.dumpmachine) to print the state of the quantum computer.\n", "The exact behavior of this function depends on the quantum simulator or processor you are using.\n", "On a full state simulator used in this demo, this function prints the information on each basis state, one basis state per row.\n", "This includes information about the amplitude of the state, the probability of measuring that state, and the phase of the state (more on that later).\n", "Each row has the following format:\n", "\n", "![DumpMachine header](./img/Dumpmachine-visualization-state-header.png)\n", "\n", "For example, the state $|0\\rangle$ would be represented as follows:\n", "\n", "![0 state](./img/Dumpmachine-visualization-state-0.png)\n", "\n", "The state $\\frac{1}{\\sqrt{2}}|0\\rangle - \\frac{i}{\\sqrt{2}}|1\\rangle$ would be represented as so:\n", "\n", "![-i state](./img/Dumpmachine-visualization-state--i.png)\n", "\n", "\n", "> It is important to note that although we reason about quantum systems in terms of their state, Q# does not have any representation of the quantum state in the language. Instead, state is an internal property of the quantum system, modified using gates. For more information, see [Q# documentation on quantum states](https://docs.microsoft.com/azure/quantum/concepts-dirac-notation#q-gate-sequences-equivalent-to-quantum-states).\n", "\n", "This demo shows how to allocate a qubit and examine its state in Q#. This demo uses quantum gates to manipulate the state of the qubit - we will explain how they work in the next tutorial, so do not worry about them for now. Run the next cell using `Ctrl+Enter` (`⌘+Enter` on Mac), then run the cell after it to see the output." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "// Run this cell using Ctrl+Enter (⌘+Enter on Mac)\n", "// Then run the next cell to see the output\n", "\n", "open Microsoft.Quantum.Diagnostics;\n", "\n", "operation QubitsDemo () : Unit {\n", " // This line allocates a qubit in state |0⟩\n", " use q = Qubit();\n", " Message(\"State |0⟩:\");\n", "\n", " // This line prints out the state of the quantum computer\n", " // Since only one qubit is allocated, only its state is printed\n", " DumpMachine();\n", "\n", " // This line changes the qubit from state |0⟩ to state |1⟩\n", " X(q);\n", "\n", " Message(\"State |1⟩:\");\n", " DumpMachine();\n", "\n", " // This line changes the qubit to state |-⟩ = (1/sqrt(2))(|0⟩ - |1⟩)\n", " // That is, this puts the qubit into a superposition\n", " // 1/sqrt(2) is approximately 0.707107\n", " H(q);\n", "\n", " Message(\"State |-⟩:\");\n", " DumpMachine();\n", "\n", " // This line changes the qubit to state |-i⟩ = (1/sqrt(2))(|0⟩ - i|1⟩)\n", " S(q);\n", "\n", " Message(\"State |-i⟩:\");\n", " DumpMachine();\n", "\n", " // This will put the qubit into an uneven superposition,\n", " // where the amplitudes of |0⟩ and |1⟩ have different moduli\n", " Rx(2.0, q);\n", " Ry(1.0, q);\n", "\n", " Message(\"Uneven superposition state:\");\n", " DumpMachine();\n", "\n", " // This line returns the qubit to state |0⟩\n", " Reset(q);\n", "}" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%simulate QubitsDemo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Relative and Global Phase\n", "\n", "You may recall that a complex number has a parameter called its phase. If a complex number $x$ is written in [polar form](../ComplexArithmetic/ComplexArithmetic.ipynb#Polar-coordinates) $x = re^{i\\theta}$, its phase is $\\theta$.\n", "\n", "The phase of a basis state is the complex phase of the amplitude of that state. For example, a system in state $\\frac{1 + i}{2}|0\\rangle + \\frac{1 - i}{2}|1\\rangle$, the phase of $|0\\rangle$ is $\\frac{\\pi}{4}$, and the phase of $|1\\rangle$ is $-\\frac{\\pi}{4}$. The difference between these two phases is known as **relative phase**.\n", "\n", "Multiplying the state of the entire system by $e^{i\\theta}$ doesn't affect the relative phase: $\\alpha|0\\rangle + \\beta|1\\rangle$ has the same relative phase as $e^{i\\theta}\\big(\\alpha|0\\rangle + \\beta|1\\rangle\\big)$. In the second expression, $\\theta$ is known as the system's **global phase**.\n", "\n", "The state of a qubit (or, more generally, the state of a quantum system) is defined by its relative phase - global phase arises as a consequence of using linear algebra to represent qubits, and has no physical meaning. That is, applying a phase to the entire state of a system (multiplying the entire vector by $e^{i\\theta}$ for any real $\\theta$) doesn't actually affect the state of the system. Because of this, global phase is sometimes known as **unobservable phase** or **hidden phase**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conclusion\n", "\n", "This should be enough for you to gain a basic understanding of qubits and qubit states. Next, you will learn how to manipulate those states in the [single-qubit gates tutorial](../SingleQubitGates/SingleQubitGates.ipynb)." ] } ], "metadata": { "kernelspec": { "display_name": "Q#", "language": "qsharp", "name": "iqsharp" }, "language_info": { "file_extension": ".qs", "mimetype": "text/x-qsharp", "name": "qsharp", "version": "0.14" } }, "nbformat": 4, "nbformat_minor": 2 }