Skip to main content

View on GitHub

Open this notebook in GitHub to run it yourself
“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. Similarly to classical random walks, quantum walks are divided into the discrete and continuous cases. This notebook focuses on discrete quantum walks, and is organized as follows:
  1. One to one compares classical random walks with quantum walks via a specific example: a walk on a circle.
  2. A general quantum model studies quantum walks and subsequently applies it to the circle case and to a hypercube graph.
  3. An appendix returns to the circle walk to demonstrate the synthesis engine’s optimization capabilities, comparing CX counts of two increment implementations under a width constraint.
This tutorial demonstrates the following concepts of Classiq:
  • The resemblance between classical and quantum programming with Classiq.
  • Classiq’s built-in constructs:
  • control: general controlled-logic
  • power: “parametric power”, specified as execution parameter.
  • numeric quantum types: working with signed and unsigned integers.
  • Passing a list of QCallables (quantum functions).
  • Parsed execution results, according to quantum variable types.
  • The synthesis engine’s optimization capabilities, by comparing the CX-counts of two increment implementations under a width constraint (see the appendix).

Classical Random Walks Versus Quantum Walks

Specify and implement a simple example: a discrete walk on a circle, or more precisely, on a regular polygon with 2N2^N nodes (see Figure 1). The idea behind the random/quantum walk:
  1. Start at some initial point, e.g., the zeroth node.
  2. Flip a coin.
Heads moves one step clockwise; tails moves counterclockwise.
  1. Repeat steps 1-2 for a given total number of steps TT.

Figure

  1. A graph for a random walk on a circle.
Code a classical random walk and a discrete quantum walk side by side: Define a function to flip a coin:
  • Classical: the coin is either 0 or
  1. To flip the coin, draw a random number from the set {0,1}\{0,1\}.
  • 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⟩|0\rangle state into an equal superposition of ∣0⟩|0\rangle and ∣1⟩|1\rangle.
Next, define a function for moving clockwise and counterclockwise. This operation is a modular addition by ±1\pm 1:
  • Classical: the position is an integer in [−2N−1,2N−1−1][-2^{N-1}, 2^{N-1}-1].
Use basic arithmetic operations.
  • Quantum: the position is an NN-qubits state.
Call in-place modular addition by
  1. Note that since quantum operations are reversible, you can define a counterclockwise step as the inverse of the clockwise step.
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 xx.
Define and run a specific example, taking a circle of size 272^7, a total time of 50 steps, and 10,000 samples.
Output:
Output:
Screenshot 2025-07-23 at 22.52.24.png

Figure

  1. The circuit for a quantum walk on a circle with $2^7$ nodes.

The last three blocks repeat t times.

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=50T=50.
Output:
output 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
  1. This is a small example of the different behaviors of classical random walks and quantum walks.
More details and examples are in Ref. [1].

Building a General Quantum Walk with Classiq

Define a quantum function for a discrete quantum walk. The arguments of the function:
  • time: an integer for the number of walking steps.
  • coin_flip_qfunc: the quantum function for “flipping” the coin.
  • walks_qfuncs: a list of quantum functions for all possible transitions at a given point.
  • coin_state: the quantum state of the coin.

Example: Symmetric Quantum Walk on a Circle

As a first example, consider the circle geometry above, implemented with the generic definition but with a different initial condition for the coin. Take 12(∣0⟩+i∣1⟩)\frac{1}{\sqrt{2}}(|0\rangle +i |1\rangle) instead of ∣0⟩|0\rangle. This state is a balanced initial condition for the coin (see Ref. [1]) and can be prepared by applying an H gate followed by an S gate.
Now that the model is defined, you can synthesize, execute, and plot the outcome probability:
Output:
Output:
Output:
output 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.

Example: 4D Hypercube with a Grover Coin

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 vv starting from an initial node uu. 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]) 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]. Define Pcorner(T)P_{\rm corner}(T) as the probability of measuring the walker at the opposite corner ∣2N−1⟩|2^{N}-1\rangle after TT steps, starting at position ∣0⟩|0\rangle at time
  1. Examine this quantity for the quantum and the classical case.

Figure

  1. A graph for a random walk on a 3D hypercube.
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 dd-dimensional hypercube, at each node there are dd 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 NN qubits. For the coin operator, take the “Grover diffuser” function. This choice refers to a symmetric quantum walk [1]. The Grover diffuser operator is a reflection around a given state ∣ψ⟩|\psi\rangle: G=I−2∣ψ⟩⟨ψ∣,G = I-2 |\psi\rangle\langle\psi|, where I is the identity matrix. In the NN-dimensional hypercube each node is connected to NN nodes, thus, the coin state should include NN different states. Therefore, the coin is represented by a quantum variable of size ⌈log⁡2(N)⌉\lceil \log_2(N)\rceil, and the Grover diffuser is defined with ∣ψ⟩=1N∑i=0N−1∣i⟩.|\psi\rangle = \frac{1}{\sqrt{N}}\sum^{N-1}_{i=0} |i\rangle. Build the model using the grover_diffuser function from the Classiq open library.
Output:
Screenshot 2025-06-19 at 23.59.11.png

Figure

  1. The circuit for a quantum walk on a 4D cube with a Grover coin.

The last five blocks repeat t times.

Output:
Output:
At T=4T=4, the probability to measure the walker at the opposite corner is larger than 1/21/2. Check an analogous question in the classical random walk, where the distribution is taken over an ensemble of independent experiments:
Output:
For the classical analogous case, the probability is much lower. This is a manifestation of the fact that the hitting time in a hypercube is exponentially shorter in quantum walks, in comparison to classical random walks. [1]: Kempe, J. “Quantum random walks: An introductory overview.” Contemporary Physics 44, 307 (2003). [2]: Kempe, J. “Discrete quantum walks hit exponentially faster.” Probability theory and related fields 133, 215 (2005).

Appendix: CX-Count Comparison for the Circle Increment

Return to the circle walk of the first section and examine the synthesis engine’s optimization capabilities. The core building block of a single walk step is the increment function that moves the position one step clockwise or counterclockwise. Compare two implementations of this increment:
  • Built-in increment: the high-level x += 1 used earlier via quantum_step_clockwise.
  • Naive increment: a manual cascade of multi-controlled-X (MCX) gates, as used in the Discrete Quantum Walk on a Circle notebook.
For a fixed circle size of 2102^{10} nodes, synthesize a single walk step with CX-count optimization under increasing width constraints, and compare the resulting CX counts for the two increment implementations.
Output:
Plot the CX counts as a function of the width constraint, for the fixed circle size of 2102^{10} nodes:
Output:
output The built-in increment’s CX-count keeps dropping as more width is allowed, since the synthesis engine can trade the extra qubits for a cheaper implementation. The naive MCX cascade improves too, but with higher counts and only up to a point (width 15): past that, the engine has no further use for the additional qubits. This illustrates the benefit of using high-level constructs, such as x += 1, over a manually chosen circuit - the former gives the synthesis engine more room to optimize.