Note
Go to the end to download the full example code.
Large-scale simulations with tensor network simulatorΒΆ
In this example, we demonstrate simulation of MBQC involving 10k+ nodes.
Firstly, let us import relevant modules and define the circuit:
from __future__ import annotations
from typing import TYPE_CHECKING
from graphix import Circuit
from graphix.fundamentals import ANGLE_PI
if TYPE_CHECKING:
from graphix.fundamentals import Angle
def cp(circuit: Circuit, theta: Angle, control: int, target: int) -> None:
circuit.rz(control, theta / 2)
circuit.rz(target, theta / 2)
circuit.cnot(control, target)
circuit.rz(target, -1 * theta / 2)
circuit.cnot(control, target)
def qft_rotations(circuit: Circuit, n: int) -> None:
circuit.h(n)
for qubit in range(n + 1, circuit.width):
cp(circuit, ANGLE_PI / 2 ** (qubit - n), qubit, n)
def swap_registers(circuit: Circuit, n: int) -> None:
for qubit in range(n // 2):
circuit.swap(qubit, n - qubit - 1)
def qft(circuit: Circuit, n: int) -> None:
for i in range(n):
qft_rotations(circuit, i)
swap_registers(circuit, n)
We will simulate 55-qubit QFT, which requires graph states with more than 10000 nodes.
n = 55
print(f"{n}-qubit QFT")
circuit = Circuit(n)
for i in range(n):
circuit.h(i)
qft(circuit, n)
# standardize pattern
pattern = circuit.transpile().pattern
pattern.standardize()
pattern.shift_signals()
graph = pattern.extract_graph()
print(f"Number of nodes: {len(graph.nodes)}")
print(f"Number of edges: {len(graph.edges)}")
55-qubit QFT
Number of nodes: 15015
Number of edges: 17930
Using efficient graph state simulator graphix.graphsim, we can classically preprocess Pauli measurements.
We are currently improving the speed of this process by using rust-based graph manipulation backend.
pattern.remove_input_nodes()
pattern.perform_pauli_measurements()
To specify TN backend of the simulation, simply provide as a keyword argument. here we do a very basic check that one of the statevector amplitudes is what it is expected to be:
import time
t1 = time.time()
tn = pattern.simulate_pattern(backend="tensornetwork")
value = tn.basis_amplitude(0)
t2 = time.time()
print("amplitude of |00...0> is ", value)
print("1/2^n (true answer) is", 1 / 2**n)
print("approximate execution time in seconds: ", t2 - t1)
/home/docs/checkouts/readthedocs.org/user_builds/graphix/checkouts/stable/examples/qft_with_tn.py:80: UserWarning: Default random-number generator is used. Results may not be reproducible.
tn = pattern.simulate_pattern(backend="tensornetwork")
amplitude of |00...0> is 2.7755575615629394e-17
1/2^n (true answer) is 2.7755575615628914e-17
approximate execution time in seconds: 12.446928262710571
Total running time of the script: (0 minutes 46.256 seconds)