Skip to content

Commit

Permalink
Merge branch 'Classiq:main' into generative
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandre-ricardo authored Dec 19, 2024
2 parents 04de26a + 68deba4 commit bd03ba3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
2 changes: 2 additions & 0 deletions requirements_tests.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pytest

testbook

torch
torchvision
galois
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"id": "122955ef-8431-44dc-a511-603e9f5f417d",
"metadata": {},
"source": [
"\"Quantum Walk\" is an approach for developing and designing quantum algorithms. It can be thought as a quantum analogy to classical random walk. This generic technique underlies many quantum algorithms, in particular, the Grover's search algorithm, can be viewed as a quantum walk.\n",
"\"Quantum Walk\" is an approach for developing and designing quantum algorithms. Consider it as a quantum analogy to the classical random walk. This generic technique underlies many quantum algorithms. In particular, the Grover's search algorithm can be viewed as a quantum walk.\n",
"\n",
"In similar to classical random walks, quantum walks are divided into the discrete case and the continuos one. This notebook focuses on discrete quantum walks, and is organized as follows: First, we give a [one to one](##Classical-Random-Walks-vs.-Quantum-Walks) comparison between classical random walks and quantum walks, by treating a specific example: a walk on a circle. Then, we define a [general quantum model](##How-to-Build-a-general-Quantum-Walk-with-Classiq) for studying quantum walks, and consequently apply it to the [circle case](##Example:-Symmetric-Quantum-Walk-on-a-Circle) and to a a [hypercube graph](##Example:-4D-Hypercube-with-a-Grover-Coin). "
"Similarly to classical random walks, quantum walks are divided into the discrete case and the continuous one. This notebook focuses on discrete quantum walks, and is organized as follows: first, [one to one](##Classical-Random-Walks-vs.-Quantum-Walks) compares classical random walks with quantum walks via a specific example: a walk on a circle. Then, a [general quantum model](##How-to-Build-a-general-Quantum-Walk-with-Classiq) studies quantum walks and subsequently applies it to the [circle case](##Example:-Symmetric-Quantum-Walk-on-a-Circle) and to a [hypercube graph](##Example:-4D-Hypercube-with-a-Grover-Coin). "
]
},
{
Expand All @@ -42,18 +42,18 @@
"id": "07459d9f-0997-40d6-bc15-ce34daf19a13",
"metadata": {},
"source": [
"## Classical Random Walks vs. Quantum Walks"
"## Classical Random Walks Versus Quantum Walks"
]
},
{
"cell_type": "markdown",
"id": "8e415615-4bc5-4b8e-8090-a73493e8d6bb",
"metadata": {},
"source": [
"We first specify and implement a simple example: a discrete walk on a circle, or more precisely, on a regular polygon having $2^N$ nodes (see Figure 1 below). The idea of random/quantum walk is the following:\n",
"1. We start at some initial point, e.g., the zeroth node.\n",
"2. We flip a coin, if we get heads/tails we move one step clockwise/counter-clockwise.\n",
"3. We repeat steps 1-2 for a given total number of steps $T$."
"Specify and implement a simple example: a discrete walk on a circle, or more precisely, on a regular polygon with $2^N$ nodes (see Figure 1). The idea behind the random/quantum walk:\n",
"1. Start at some initial point, e.g., the zeroth node.\n",
"2. Flip a coin. Heads moves one step clockwise; tails moves counterclockwise.\n",
"3. Repeat steps 1-2 for a given total number of steps $T$."
]
},
{
Expand Down Expand Up @@ -92,17 +92,17 @@
"id": "ad402658-6f37-4013-b345-61a608560025",
"metadata": {},
"source": [
"Let us code a classical random walk and a discrete quantum walk side by side:"
"Code a classical random walk and a discrete quantum walk side by side:"
]
},
{
"cell_type": "markdown",
"id": "7fa60d50-5633-4458-9a9a-c908cde93a52",
"metadata": {},
"source": [
"First, we define a function to flip a coin:\n",
"* Classical: the coin is either 0 or 1, to flip a coin we simply draw a random number from the set $\\{0,1\\}$.\n",
"* Quantum: the coin is represented by a qubit, a \"flip\" is defined by some unitary operation on it. We choose the Hadamard gate, which sends the $|0\\rangle$ state into an equal superposition of $|0\\rangle$ and $|1\\rangle$."
"Define a function to flip a coin:\n",
"* Classical: the coin is either 0 or 1. To flip the coin, draw a random number from the set $\\{0,1\\}$.\n",
"* Quantum: the coin is represented by a qubit and a \"flip\" is defined by some unitary operation on it. Choose the Hadamard gate, which sends the $|0\\rangle$ state into an equal superposition of $|0\\rangle$ and $|1\\rangle$."
]
},
{
Expand Down Expand Up @@ -133,9 +133,9 @@
"id": "d10a317a-0267-4b51-b230-3d3cd393d3fd",
"metadata": {},
"source": [
"Next, we define a function for moving clockwise and counterclockwise. This operation is simply a modular addition by $\\pm 1$.\n",
"* Classical: the position is an integer in $[-2^{N-1}, 2^{N-1}-1]$, we use basic arithmetic operations.\n",
"* Quantum: the position is an $N$-qubits state. We can easily build an inplace modular addition by 1 ourselves (see explanation at [the end of this notebook](#Technical-Notes)). Note that since quantum operations are reversible, we can define a counterclockwise step as the inverse of the clockwise step."
"Next, define a function for moving clockwise and counterclockwise. This operation is a modular addition by $\\pm 1$.\n",
"* Classical: the position is an integer in $[-2^{N-1}, 2^{N-1}-1]$. Use basic arithmetic operations.\n",
"* Quantum: the position is an $N$-qubits state. Build an in-place modular addition by 1 (see an explanation at [the end of this notebook](#Technical-Notes)). Note that since quantum operations are reversible, you can define a counterclockwise step as the inverse of the clockwise step."
]
},
{
Expand Down Expand Up @@ -179,7 +179,7 @@
"id": "9f102eaf-80e8-45d3-8b55-e09ede9604ec",
"metadata": {},
"source": [
"Finally, we construct a function for the full walk, iterating over flipping a coin and a single walking step based on the coin's state. Note the difference between the classical and quantum functions' declarations, for the quantum part we do not need to pass the circle size, as it is given by the size of the position state $x$."
"Finally, construct a function for the full walk, iterating between flipping a coin and a single walking step based on the coin's state. Note the difference between the classical and quantum function declarations. For the quantum part you do not need to pass the circle size, as it is given by the size of the position state $x$."
]
},
{
Expand Down Expand Up @@ -237,7 +237,7 @@
"id": "ea9266b8-60a0-4f93-bc6b-942cd6d85cb7",
"metadata": {},
"source": [
"Let us define and run a specific example. We take a circle of size $2^7$, a total time of 50 steps, and 10000 samples. "
"Define and run a specific example, taking a circle of size $2^7$, a total time of 50 steps, and 10000 samples. "
]
},
{
Expand Down Expand Up @@ -332,7 +332,7 @@
"id": "4237a7e6-9dd1-4580-b3cf-22282e341909",
"metadata": {},
"source": [
"We can plot the probabilities of ending at each position along the circle. In both cases, classical and quantum, we consider the probability of the final position after time $T=50$."
"Plot the probabilities of ending at each position along the circle. In both the classical and quantum cases, consider the probability of the final position after time $T=50$."
]
},
{
Expand Down Expand Up @@ -399,7 +399,7 @@
"id": "922b31e2-d1c4-4d7d-afd1-c633b5981441",
"metadata": {},
"source": [
"We can see a clear difference between the two distributions. The classical distribution is symmetric around the zero position, whereas the quantum example is asymmetric with a peak far from 0. This is a small example of the different behaviors of classical random walks and quantum walks. More details and examples can be found in Ref. [[1](#review)]."
"There is a clear difference between the two distributions. The classical distribution is symmetric around the zero position, whereas the quantum example is asymmetric with a peak far from 0. This is a small example of the different behaviors of classical random walks and quantum walks. More details and examples are in Ref. [[1](#review)]."
]
},
{
Expand All @@ -415,11 +415,11 @@
"id": "10f78850-0b2e-40ae-bd19-9558f6da238c",
"metadata": {},
"source": [
"We define a quantum function for a discrete quantum walk. The arguments of the function are:\n",
"Define a quantum function for a discrete quantum walk. The arguments of the function:\n",
"* `time`: an integer for the number of walking steps.\n",
"* `coin_flip_qfunc`: the quantum function for \"flipping\" the coin.\n",
"* `walks_qfuncs`: a list of quantum functions for all possible transitions at a given point.\n",
"* `coin_state`: the quantum state of the coin."
"* `walks_qfuncs`: a list of quantum functions for all possible transitions at a given point.\n",
"* `coin_state`: the quantum state of the coin."
]
},
{
Expand Down Expand Up @@ -472,7 +472,7 @@
"id": "5cc94bcc-8d84-407d-9e76-6137899327df",
"metadata": {},
"source": [
"As a first example, we can consider the circle geometry from above, implemented with our generic definition. However, as opposed to the implementation above we take a different initial condition for the coin, $\\frac{1}{\\sqrt{2}}(|0\\rangle +i |1\\rangle)$ instead of $|0\\rangle$. This state is a balanced initial condition for the coin (see Ref. [[1](#review)]) and can be prepared by applying an H gate followed by an S gate."
"As a first example, consider the circle geometry above, implemented with the generic definition but with a different initial condition for the coin. Take $\\frac{1}{\\sqrt{2}}(|0\\rangle +i |1\\rangle)$ instead of $|0\\rangle$. This state is a balanced initial condition for the coin (see Ref. [[1](#review)]) and can be prepared by applying an H gate followed by an S gate."
]
},
{
Expand Down Expand Up @@ -531,7 +531,7 @@
"id": "7334aee1-b637-419d-88e3-130b66c1140c",
"metadata": {},
"source": [
"Now that our model is defined we can synthesize, execute and plot the outcome probability:"
"Now that the model is defined, you can synthesize, execute, and plot the outcome probability:"
]
},
{
Expand Down Expand Up @@ -596,7 +596,7 @@
"id": "233124ab-fe1e-413c-ac05-ac72cc723e19",
"metadata": {},
"source": [
"We can see that now the distribution is symmetric. Clearly, the distribution of a quantum walk on a circle depends on the initial condition for the coin. This is another example of the peculiar behavior of quantum walks, with respect to classical random walks."
"The distribution is symmetric. Clearly, the distribution of a quantum walk on a circle depends on the initial condition for the coin. This is another example of the peculiar behavior of quantum walks compared to classical random walks."
]
},
{
Expand All @@ -612,9 +612,9 @@
"id": "ab34bdd9-c5aa-4744-aa41-4831bf88117d",
"metadata": {},
"source": [
"Next, we consider an hypercube graph (see Figure 3). The quantum walk on a hypercube shows a completely different behavior compared to classical counterpart. One striking difference refers to the hitting time: the time it takes to reach node $v$ starting from an initial node $u$. In particular, an interesting question is the hitting time between one corner of the hypercube \"000..0\" to the opposite one \"111...1\". In Ref. [[2](#hypercube)]) it was shown that the hitting time for the hypercube in quantum walks is *exponentially faster* than the analog quantity in classical random walks. The rigour definition of the \"hitting time\" in the quantum case is nontrivial, and different definitions are relevant. In this notebook we do not use the exact definition, but simply demonstrate a specific result, highlighting the result of Ref. [[2](#hypercube)]. \n",
"Next, consider a hypercube graph (see Figure 3). The quantum walk on a hypercube shows completely different behavior compared to its classical counterpart. One striking difference refers to the hitting time, which is the time it takes to reach node $v$ starting from an initial node $u$. In particular, an interesting question is the hitting time between one corner of the hypercube \"000..0\" to the opposite one \"111...1\". Ref. [[2](#hypercube)]) shows that the hitting time for the hypercube in quantum walks is *exponentially faster* than the analog quantity in classical random walks. The rigorous definition of the \"hitting time\" in the quantum case is nontrivial, and different definitions are relevant. This notebook does not use the exact definition, but simply demonstrates a specific result, highlighting the result of Ref. [[2](#hypercube)]. \n",
"\n",
"We define $P_{\\rm corner}(T)$ as the probability of measuring the walker at the opposite corner $|2^{N}-1\\rangle$ after $T$ steps, starting at position $|0\\rangle$ at time 0. We examine this quantity for the quantum and the classical case."
"Define $P_{\\rm corner}(T)$ as the probability of measuring the walker at the opposite corner $|2^{N}-1\\rangle$ after $T$ steps, starting at position $|0\\rangle$ at time 0. Examine this quantity for the quantum and the classical case."
]
},
{
Expand All @@ -633,18 +633,18 @@
"id": "7dc4fde2-43ee-4249-84ee-909c3b694ce0",
"metadata": {},
"source": [
"First, we define a model for quantum walk on a hypercube. Two nodes in the hypercube are connected to each other iff their Hamming distance is 1. Thus, a step along a hypercube is given by moving \"1 Hamming distance away\". For a $d$-dimentional hypercube, at each node there are $d$ possible directions to move, each of them is given by applying a bit flip on one of the bits. In the quantum case, this is obtained by applying an X gate on on of the $N$ qubit.\n",
"Define a model for quantum walk on a hypercube. Two nodes in the hypercube are connected to each other if their Hamming distance is 1. Thus, a step along a hypercube is given by moving \"1 Hamming distance away\". For a $d$-dimentional hypercube, at each node there are $d$ possible directions to move. Each of them is given by applying a bit flip on one of the bits. In the quantum case, this is obtained by applying an X gate on one of the $N$ qubits.\n",
"\n",
"Concerning the coin operator we take the so-called \"Grover diffuser\" function. This choice refers to a symmetric quantum walk [[1](#review)]. The Grover diffuser operator is a reflection around a given state $|\\psi\\rangle$:\n",
"For the coin operator, take the \"Grover diffuser\" function. This choice refers to a symmetric quantum walk [[1](#review)]. The Grover diffuser operator is a reflection around a given state $|\\psi\\rangle$:\n",
"$$\n",
"G = I-2 |\\psi\\rangle\\langle\\psi|,\n",
"$$\n",
"where I is the identity matrix. In the $N$-dimensional hypercube each node is connected to $N$ nodes, thus, the coin state should include $N$ different states. Therefore, the coin is represented by a quantum variable of size $\\lceil \\log_2(N)\\rceil$, and the Grover diffuser is defined with:\n",
"where I is the identity matrix. In the $N$-dimensional hypercube each node is connected to $N$ nodes, thus, the coin state should include $N$ different states. Therefore, the coin is represented by a quantum variable of size $\\lceil \\log_2(N)\\rceil$, and the Grover diffuser is defined with\n",
"$$\n",
"|\\psi\\rangle = \\frac{1}{\\sqrt{N}}\\sum^{N-1}_{i=0} |i\\rangle.\n",
"$$\n",
"\n",
"We now build our model, using the `grover_diffuser` function from Classiq's open library."
"Build the model using the `grover_diffuser` function from the Classiq open library."
]
},
{
Expand Down Expand Up @@ -686,7 +686,7 @@
"SPACE_SIZE = 4\n",
"NUM_SHOTS = 1e4\n",
"\n",
"# in the case the space size is not 2^r we padd the coin state with zeros\n",
"# if the space size is not 2^r, pad the coin state with zeros\n",
"reduced_coin_state = [1 / SPACE_SIZE] * SPACE_SIZE + [0] * int(\n",
" 2 ** np.ceil(np.log2(SPACE_SIZE)) - SPACE_SIZE\n",
")\n",
Expand All @@ -698,10 +698,10 @@
" x: Output[QArray[QBit]],\n",
"):\n",
"\n",
" # start at zero state location\n",
" # start at the zero state location\n",
" allocate(SPACE_SIZE, x)\n",
"\n",
" # start with equal superpositon for the relevant states.\n",
" # start with equal superposition for the relevant states.\n",
" coin = QArray(\"coin\")\n",
" prepare_state(reduced_coin_state, 0.0, coin)\n",
" discrete_quantum_walk(\n",
Expand Down

0 comments on commit bd03ba3

Please sign in to comment.