Backends
Overview
The backend is a function to compute the result of the circuit, as the probability of a specific outcome, the expectation value of the observable, or the final state vector. There’re two computation modes to run the quantum simulation – wave function vector method and tensor network method with optimal contraction path. The computation of the tensor-dot with these two modes is based on the PyTorch and JAX computation modules embedded into the TeD-Q package.
Wave function vector method
In this mode, the quantum gate acts on the state vector in sequence so that the simulator can acquire the resulting state after each operation of the gate. However, the computation could become a memory- and time-consuming job. With this method, a typical laptop CPU can deal with a simple quantum circuit of up to 20 qubits.
Tensor network method
The simulation of the quantum circuit can be converted into a tensor network. In this method, the sequence of tensor dot operations is reorganized with an optimizer to find the best tensor contraction order so that the computer can calculate the result with less memory and higher speed. The first step of this method is to manipulate the circuit based on the output type and convert the resulting circuit to a hypergraph, which is shown in the later section, and the detailed process of the optimizer is described in the section PathOptimizer. This method is designed for the complex quantum circuit and can be applied to the CPU and GPU clusters.
Compiled circuit for specific output type
There are three types of output from a circuit.
State before measurement
Probability of a specific outcome
The expectation value of the circuit
The compiled circuit is different for each type of output, so the optimal method to do the tensor contraction is also different. Take the simple two-qubit circuit shown in the previous section; for example, the “state before measurement” circuit is shown in the figure below.
![]()
The computer can compute the circuit in the blue box to the state vector \(|\phi\psi\rangle.\)
However, the optimal contraction method to find the state cannot be applied to the circuit of “Probability” or “Expectation value” because the circuit is different. To find the probability of a specific outcome, for instance, qubit #1 to be 0, we need to take the inner product of the measurement operator by the state vector.
\[P(A) = \Bigg\langle\phi\psi\Bigg|A\Bigg|\phi\psi\Bigg\rangle\]Therefore, the circuit becomes
![]()
and
![]()
From quantum circuit to hypergraph
The TeD-Q module can convert a quantum circuit to a graph while the quantum gate is the node in the graph. The figure below shows a sample circuit and its corresponding graph.
![]()
This graph can be fed into the optimizer to find the optimal contraction sequence. The optimizer in the package includes the well-known CoTenGra package and an improved version of it – JDOptTN.
Evaluation of the quantum circuit
CompiledCircuit
Supported Backends
JAX backend
- This is the backend embeded the JAX computation module into tedq.
The backend is used for the computation of simulation of quantum circuit, as well as finding the gradient of output result among the input parameters. To make the simulation of quantum circuit compatible to the PyTorch machine learning module, the tedq also support to include a PyTorch interface to JAX backend. Detailed can be found in
TODO- There’re three supported methods to calculate the gradient
Parameters-shift method (TBD)
Finite-differential (TBD)
Back-propagation method
- class tedq.backends.jax_backend.JaxBackend(backend, circuit, use_cotengra=False, use_jdopttn=False, tn_mode=False, hyper_opt=None, tn_simplify=False, **kwargs)
This is the backend embeded the JAX computation module into tedq. The computation function can be compiled by JIT to increase the computation speed. Besides, PyTorch interface is also included to provide compatibility to Pytorch ML module, which can be turned on by interface option.
- Args:
backend (string): Name of the computation backend –
jaxorpytorchcircuit (.Circuit): Circuit to be computed. use_cotengra (Bool): Whether to use cotengra optimizer or not. use_jdopttn (Bool): Whether to use cotengra optimizer or not. hyper_opt (dict): TODO slice options kwargs (dict): Other keyword arguments
- _matrix_to_tensor(matrix, num_qubits)
converting a numpy matrix to corresponding backend tensor
- complex_conjugate(ts)
Get complex conjugate of that tensor
- default_initstate()
Get initial state
- execute(*params)
execution function
- classmethod get_CNOT_tensor(paramslist)
Get corresponding tensor of CNOT gate
See
get_CNOT_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CRX_tensor(paramslist)
Get corresponding tensor of CRX gate
See
get_CRX_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CRY_tensor(paramslist)
Get corresponding tensor of CRY gate
See
get_CRY_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CRZ_tensor(paramslist)
Get corresponding tensor of CRZ gate
See
get_CRZ_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CSWAP_tensor(paramslist)
Get corresponding tensor of C-swap gate
See
get_CSWAP_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CY_tensor(paramslist)
Get corresponding tensor of CY gate
See
get_CY_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CZ_tensor(paramslist)
Get corresponding tensor of CZ gate
See
get_CZ_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_ControlledPhaseShift_tensor(paramslist)
Get corresponding tensor of controlled-phase-shift gate
See
get_ControlledPhaseShift_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_Hadamard_tensor(paramslist)
Get corresponding tensor of Hadamard gate
See
get_Hadamard_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_I_tensor(paramslist)
Get corresponding tensor of I gate
See
get_I_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_PauliX_tensor(paramslist)
Get corresponding tensor of Pauli-X gate
See
get_PauliX_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_PauliY_tensor(paramslist)
Get corresponding tensor of Pauli-Y gate
See
get_PauliY_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_PauliZ_tensor(paramslist)
Get corresponding tensor of Pauli-Z gate
See
get_PauliZ_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_PhaseShift_tensor(paramslist)
Get corresponding tensor of phase-shift gate
See
get_PhaseShift_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_RX_tensor(paramslist)
Get corresponding tensor of RX gate
See
get_RX_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_RY_tensor(paramslist)
Get corresponding tensor of RY gate
See
get_RY_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_RZ_tensor(paramslist)
Get corresponding tensor of RZ gate
See
get_RZ_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_Rot_tensor(paramslist)
Get corresponding tensor of Rot gate
See
get_Rot_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_SWAP_tensor(paramslist)
Get corresponding tensor of swap gate
See
get_SWAP_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_SX_tensor(paramslist)
Get corresponding tensor of SX gate
See
get_SX_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_S_tensor(paramslist)
Get corresponding tensor of S gate
See
get_S_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_T_tensor(paramslist)
Get corresponding tensor of T gate
See
get_T_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_Toffoli_tensor(paramslist)
Get corresponding tensor of Toffoli gate
See
get_Toffoli_tensor()inCompiledCircuitclass for more detailed information.
- get_initstate()
- get_measurement_results(state)
from the final quantum circuit state, calculate the measurement result
- svpm_mode()
state vector propagation mode
- tedq.backends.jax_backend.core_contract(operands, axeslist, permutationlist)
core contract for state vector propagation mode
PyTorch backend
- This is the backend embeded the PyTorch computation module into tedq.
- The backend is used for the computation of simulation of quantum circuit,
as well as finding the gradient of output result among the input parameters.
- There’re three supported methods to calculate the gradient
Parameters-shift method
Finite-differential
Back-propagation method
- class tedq.backends.pytorch_backend.PyTorchBackend(backend, circuit, use_cotengra=False, use_jdopttn=False, tn_mode=False, hyper_opt=None, tn_simplify=True, **kwargs)
pytorch backend to do the calculation.
- _device = None
This is the backend embeded the PyTorch computation module into tedq.
- Args:
backend (string): Name of the computation backend –
jaxorpytorchcircuit (.Circuit): Circuit to be computed. use_cotengra (Bool): Whether to use cotengra optimizer or not. use_jdopttn (Bool): Whether to use cotengra optimizer or not. hyper_opt (dict): TODO slice options kwargs (dict): Other keyword arguments
- _matrix_to_tensor(matrix, num_qubits)
converting a numpy matrix to corresponding backend tensor
- check_parameters_torch_device(params)
An auxiliary function to check the Torch device specified for the input patameters.
- Args:
params (list[torch.tensor]): list of parameters to check
- complex_conjugate(ts)
Get complex conjugate of that tensor
- default_initstate()
Initial all \(|0\rangle\) state
- property device
return the device used for calculation, GPU or CPU
- execute(*params)
Execute quantum circuit and get the gradient of the parameters with back-propagation method.
- finite_diff_execute(*params)
Execute quantum circuit and get the gradient of the parameters with finite differential method.
- classmethod get_CNOT_tensor(paramslist)
Get corresponding tensor.
See
get_CNOT_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CRX_tensor(paramslist)
Get corresponding tensor.
See
get_CRX_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CRY_tensor(paramslist)
Get corresponding tensor.
See
get_CRY_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CRZ_tensor(paramslist)
Get corresponding tensor.
See
get_CRZ_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CSWAP_tensor(paramslist)
Get corresponding tensor.
See
get_CSWAP_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CY_tensor(paramslist)
Get corresponding tensor.
See
get_CY_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_CZ_tensor(paramslist)
Get corresponding tensor.
See
get_CZ_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_ControlledPhaseShift_tensor(paramslist)
Get corresponding tensor.
See
get_ControlledPhaseShift_tensor()in Compliedfor more detailed information.Circuit class
- classmethod get_Hadamard_tensor(paramslist)
Get corresponding tensor.
See
get_Hadamard_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_I_tensor(paramslist)
Get corresponding tensor. .
See
get_I_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_PauliX_tensor(paramslist)
Get corresponding tensor.
See
get_PauliX_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_PauliY_tensor(paramslist)
Get corresponding tensor.
See
get_PauliY_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_PauliZ_tensor(paramslist)
Get corresponding tensor.
See
get_PauliZ_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_PhaseShift_tensor(paramslist)
Get corresponding tensor.
See
get_PhaseShift_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_RX_tensor(paramslist)
Get corresponding tensor.
See
get_RX_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_RY_tensor(paramslist)
Get corresponding tensor.
See
get_RY_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_RZ_tensor(paramslist)
Get corresponding tensor.
See
get_RZ_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_Rot_tensor(paramslist)
Get corresponding tensor of Rot gate
See
get_Rot_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_SWAP_tensor(paramslist)
Get corresponding tensor.
See
get_SWAP_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_SX_tensor(paramslist)
Get corresponding tensor.
See
get_SX_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_S_tensor(paramslist)
Get corresponding tensor.
See
get_S_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_T_tensor(paramslist)
Get corresponding tensor.
See
get_T_tensor()inCompiledCircuitclass for more detailed information.
- classmethod get_Toffoli_tensor(paramslist)
Get corresponding tensor.
See
get_Toffoli_tensor()inCompiledCircuitclass for more detailed information.
- get_initstate()
- get_measurement_results(state)
get measurement rusults based on final quantum circuit state
- get_trainable_parameters()
return trainable parameters
- jacobian_param_shift(params)
Get jacobian vectors of quantum circuits by the parameter shift rule
- param_shift_execute(*params)
Execute quantum circuit and get the gradient of the parameters with parameter shift method
- property states_after_measurement
- classmethod update_device(device)
- class tedq.backends.pytorch_backend.TorchExecute(*args, **kwargs)
Custom autograd functions for parameter-shift method to provide compatibility of backpropagtion in PyTorch ML function. Use ‘Function.apply’ to run the function, where any arguments can be set as long as having the same format as the ‘forward’ function.
- _backward_cls
alias of
TorchExecuteBackward
- static backward(ctx, dy_)
backward function
- static forward(ctx, input_kwargs, *input_params)
forawrd function



