Tensor network backend

Simulating quantum circuits using tensor networks has been studied in the literature[Markov2008][Pan2022]. The YaoToEinsum package provides a convenient way to convert Yao circuits to tensor networks, which can be used for further analysis and optimization.


The main function is

yao2einsum(circuit; initial_state=Dict(), final_state=Dict(), optimizer=TreeSA())

which transforms a Yao circuit to a tensor network that generalizes the hyper-graph (einsum notation). The return value is a TensorNetwork object.

  • initial_state and final_state are for specifying the initial state and final state. Left the qubits unspecified if you want to keep them as the open indices.
  • optimizer is for specifying the contraction order optimizing algorithm of the tensor network. The default value is the TreeSA() algorithm that developed in [Kalachev2021][Liu2023]. Please check the README of OMEinsumEinsumContractors.jl for more information.

In the following example, we show how to convert a quantum Fourier transform circuit to a tensor network and contract it to

  • Get the matrix representation of the circuit.
  • Get the probability of measuring the zero state after applying the circuit on the zero state.
julia> import Yao
julia> using Yao.EasyBuild: qft_circuit
julia> n = 10;
julia> circuit = qft_circuit(n); # build a quantum Fourier transform circuit
julia> network = Yao.yao2einsum(circuit) # convert this circuit to tensor networkTensorNetwork Time complexity: 2^20.061761433778916 Space complexity: 2^20.0 Read-write complexity: 2^20.12126537486653
julia> reshape(Yao.contract(network), 1<<n, 1<<n) ≈ Yao.mat(circuit)true
julia> network = Yao.yao2einsum(circuit; # convert circuit sandwiched by zero states initial_state=Dict([i=>0 for i=1:n]), final_state=Dict([i=>0 for i=1:n]), optimizer=Yao.YaoToEinsum.TreeSA(; nslices=3)) # slicing techniqueTensorNetwork Time complexity: 2^12.192292814470768 Space complexity: 2^5.0 Read-write complexity: 2^13.00842862207058
julia> Yao.contract(network)[] ≈ Yao.zero_state(n)' * (Yao.zero_state(n) |> circuit)true


yao2einsum(circuit; initial_state=Dict(), final_state=Dict(), optimizer=TreeSA())
yao2einsum(circuit, initial_state::Dict, final_state::Dict, optimizer)

Transform a Yao circuit to a generalized tensor network (einsum) notation. The return value is a TensorNetwork instance.


  • circuit is a Yao block as the input.
  • initial_state and final_state are dictionaries to specify the initial states and final states (taking conjugate).
    • In the first interface, a state is specified as an integer, e.g. Dict(1=>1, 2=>1, 3=>0, 4=>1) specifies a product state |1⟩⊗|1⟩⊗|0⟩⊗|1⟩.
    • In the second interface, a state is specified as an ArrayReg, e.g. Dict(1=>rand_state(1), 2=>rand_state(1)).

If any qubit in initial state or final state is not specified, it will be treated as a free leg in the tensor network.

  • optimizer is the optimizer used to optimize the tensor network. The default is TreeSA().

Please check OMEinsumContractors.jl for more information.

julia> using Yao

julia> c = chain(3, put(3, 2=>X), put(3, 1=>Y), control(3, 1, 3=>Y))
nqubits: 3
├─ put on (2)
│  └─ X
├─ put on (1)
│  └─ Y
└─ control(1)
   └─ (3,) Y

julia> yao2einsum(c; initial_state=Dict(1=>0, 2=>1), final_state=Dict(1=>ArrayReg([0.6, 0.8im]), 2=>1))
Time complexity: 2^4.700439718141093
Space complexity: 2^2.0
Read-write complexity: 2^6.0

A (generalized) tensor network representation of a quantum circuit.


  • code::AbstractEinsum: The einsum code.
  • tensors::Vector: The tensors in the network.


  • Pan2022Pan, Feng, and Pan Zhang. "Simulation of quantum circuits using the big-batch tensor network method." Physical Review Letters 128.3 (2022): 030501.
  • Kalachev2021Kalachev, Gleb, Pavel Panteleev, and Man-Hong Yung. "Recursive multi-tensor contraction for xeb verification of quantum circuits." arXiv preprint arXiv:2108.05665 (2021).
  • Markov2008Markov, Igor L., and Yaoyun Shi. "Simulating quantum computation by contracting tensor networks." SIAM Journal on Computing 38.3 (2008): 963-981.
  • Liu2023Liu, Jin-Guo, et al. "Computing solution space properties of combinatorial optimization problems via generic tensor networks." SIAM Journal on Scientific Computing 45.3 (2023): A1239-A1270.