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.

Tutorial

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.057737943103728 Space complexity: 2^20.0 Read-write complexity: 2^20.11353361611387
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 techniqueERROR: MethodError: no method matching TreeSA(; nslices::Int64) This error has been manually thrown, explicitly, so the method may exist but be intentionally marked as unimplemented. Closest candidates are: TreeSA(; βs, ntrials, niters, initializer, score) got unsupported keyword argument "nslices" @ OMEinsumContractionOrders ~/.julia/packages/OMEinsumContractionOrders/oVgOP/src/treesa.jl:124 TreeSA(::IT, ::Int64, ::Int64, ::Symbol, ::OMEinsumContractionOrders.ScoreFunction) where IT got unsupported keyword argument "nslices" @ OMEinsumContractionOrders ~/.julia/packages/OMEinsumContractionOrders/oVgOP/src/treesa.jl:125
julia> Yao.contract(network)[] ≈ Yao.zero_state(n)' * (Yao.zero_state(n) |> circuit)ERROR: BoundsError: attempt to access 2×2×2×2×2×2×2×2×2×2×2×2×2×2×2×2×2×2×2×2 Array{ComplexF64, 20} at index []

API

YaoToEinsum.yao2einsumFunction
yao2einsum(circuit; initial_state=Dict(), final_state=Dict(), optimizer=TreeSA(), mode=VectorMode(), observable=nothing)

Transform a Yao circuit to a generalized tensor network (einsum) notation. The return value is a TensorNetwork instance that corresponds to the following tensor network:

1). If mode is VectorMode(), the tensor network will be like:

<initial_state| ─── circuit ─── |final_state>

2). If the mode is DensityMatrixMode(), the tensor network will be like:

<final_state| ─── circuit ─── |initial_state><initial_state| ─── circuit ─── |final_state>

where the circuit may contain noise channels.

3). In the DensityMatrixMode(), if observable is specified, compute tr(rho, observable) instead.

┌── circuit ─── |initial_state><initial_state| ─── circuit ─── observable ──┐
└───────────────────────────────────────────────────────────────────────────┘

4). PauliBasisMode() is similar to the DensityMatrixMode() mode, but the basis will be rotated to the Pauli basis.

Arguments

  • mode is the mapping mode, which can be DensityMatrixMode(), PauliBasisMode(), or VectorMode().
  • 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)).
  • observable is a Yao block to specify the observable. If it is specified, the final state must be unspecified.

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
chain
├─ 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))
TensorNetwork
Time complexity: 2^4.700439718141093
Space complexity: 2^2.0
Read-write complexity: 2^6.0
source
YaoToEinsum.TensorNetworkType
TensorNetwork

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

Fields

  • code::AbstractEinsum: The einsum code.
  • tensors::Vector: The tensors in the network.
source
OMEinsumContractionOrders.optimize_codeFunction
optimize_code(c::TensorNetwork, optimizer=TreeSA(); slicer=nothing)

Optimize the code of the tensor network.

Arguments

  • c::TensorNetwork: The tensor network.
  • optimizer::Optimizer: The optimizer to use, default is OMEinsum.TreeSA().

Keyword Arguments

  • slicer: The slicer to use, default is nothing. It can be e.g. OMEinsum.TreeSASlicer(score=OMEinsum.ScoreFunction(sc_target=30)).

For more, please check OMEinsumContractionOrders documentation.

source

References

  • 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.