# cuquantum.CircuitToEinsum¶

class cuquantum.CircuitToEinsum(circuit, dtype='complex128', backend='cupy')[source]

Create a converter object that can generate Einstein summation expressions and tensor operands for a given circuit.

The supported circuit types include cirq.Circuit and qiskit.QuantumCircuit. The input circuit must be fully parameterized and can not contain operations that are not well-defined in tensor network simulation, for instance, resetting the quantum state or performing any intermediate measurement.

Parameters
• circuit – A fully parameterized cirq.Circuit or qiskit.QuantumCircuit object.

• dtype – The datatype for the output tensor operands. If not specified, double complex is used.

• backend – The backend for the output tensor operands. If not specified, cupy is used.

Notes

• For qiskit.QuantumCircuit, composite gates will be decomposed into either Qiskit standard gates or customized unitary gates.

Examples

Examples using Qiskit:

>>> import qiskit.circuit.random
>>> from cuquantum import contract, CircuitToEinsum


Generate a random quantum circuit:

>>> qc = qiskit.circuit.random.random_circuit(num_qubits=8, depth=7)


Create a CircuitToEinsum object:

>>> converter = CircuitToEinsum(qc, backend='cupy')


Find the Einstein summation expression and tensor operands for the state vector:

>>> expression, operands = converter.state_vector()


Contract the equation above to compute the state vector:

>>> sv = contract(expression, *operands)
>>> print(sv.shape)
(2, 2, 2, 2, 2, 2, 2, 2)


Find the Einstein summation expression and tensor operands for computing the probability amplitude of bitstring 00000000:

>>> expression, operands = converter.amplitude('00000000')


Contract the equation above to compute the amplitude:

>>> amplitude = contract(expression, *operands)


Find the Einstein summation expression and tensor operands for computing reduced density matrix on the first two qubits with the condition that the last qubit is fixed at state 1:

>>> where = qc.qubits[:2]
>>> fixed = {qc.qubits[-1]: '1'}
>>> expression, operands = converter.reduced_density_matrix(where, fixed=fixed)


Contract the equation above to compute the reduced density matrix:

>>> rdm = contract(expression, *operands)
>>> print(rdm.shape)
(2, 2, 2, 2)


Methods

__init__(circuit, dtype='complex128', backend='cupy')[source]
amplitude(bitstring)[source]

Generate the Einstein summation expression and tensor operands to compute the probability amplitude of a bitstring for the input circuit.

Parameters

bitstring – A sequence of 0/1 specifying the desired measured state. The order of the bitstring is expected to be consistent with CircuitToEinsum.qubits. For cirq.Circuit, this order corresponds to all qubits in the circuit sorted in ascending order. For qiskit.QuantumCircuit, this order is the same as qiskit.QuantumCircuit.qubits.

Returns

The Einstein summation expression and a list of tensor operands

batched_amplitudes(fixed)[source]

Generate the Einstein summation expression and tensor operands to compute a batch of bitstring amplitudes for the input circuit.

Parameters

fixed – A dictionary that maps certain qubits to the corresponding fixed states 0 or 1.

Returns

The Einstein summation expression and a list of tensor operands. The order of the output mode labels is consistent with CircuitToEinsum.qubits. For cirq.Circuit, this order corresponds to all qubits in the circuit sorted in ascending order. For qiskit.QuantumCircuit, this order is the same as qiskit.QuantumCircuit.qubits.

expectation(pauli_string, lightcone=True)[source]

Generate the Einstein summation expression and tensor operands to compute the expectation value of a Pauli string for the input circuit.

Unitary reverse lightcone cancellation refers to removing the identity formed by a unitary gate (from the ket state) and its inverse (from the bra state) when there exists no additional operators in-between. One can take advantage of this technique to reduce the effective network size by only including the causal gates (gates residing in the lightcone).

Parameters
• pauli_string

The Pauli string for expectation value computation. It can be:

• a sequence of characters 'I'/'X'/'Y'/'Z'. The length must be equal to the number of qubits.

• a dictionary mapping the selected qubits to Pauli characters. Qubits not specified are assumed to be applied with the identity operator 'I'.

• lightcone – Whether to apply the unitary reverse lightcone cancellation technique to reduce the number of tensors in expectation value computation.

Returns

The Einstein summation expression and a list of tensor operands.

Note

When lightcone=True, the identity Pauli operators will be omitted in the output operands. The unitary reverse lightcone cancellation technique is then applied based on the remaining causal qubits to further reduce the size of the network. The reduction effect depends on the circuit topology and the input Pauli string (so the contraction path cannot be reused for the contraction of different Pauli strings). When lightcone=False, the identity Pauli operators are preserved in the output operands such that the output tensor network has the identical topology for different Pauli strings, and the contraction path only needs to be computed once and can be reused for all Pauli strings.

reduced_density_matrix(where, fixed=None, lightcone=True)[source]

Generate the Einstein summation expression and tensor operands to compute the reduced density matrix for the input circuit.

Unitary reverse lightcone cancellation refers to removing the identity formed by a unitary gate (from the ket state) and its inverse (from the bra state) when there exists no additional operators in-between. One can take advantage of this technique to reduce the effective network size by only including the causal gates (gates residing in the lightcone).

Parameters
• where – A sequence of qubits specifying where the density matrix are reduced onto.

• fixed – Optional, a dictionary that maps certain qubits to the corresponding fixed states 0 or 1.

• lightcone – Whether to apply the unitary reverse lightcone cancellation technique to reduce the number of tensors in density matrix computation.

Returns

The Einstein summation expression and a list of tensor operands. The mode labels for output of the expression has the same order as the where argument. For example, if where = ($$a, b$$), the mode labels for the reduced density matrix would be ($$a, b, a^{\prime}, b^{\prime}$$)

state_vector()[source]

Generate the Einstein summation expression and tensor operands to compute the statevector for the input circuit.

Returns

The Einstein summation expression and a list of tensor operands. The order of the output mode labels is consistent with CircuitToEinsum.qubits. For cirq.Circuit, this order corresponds to all qubits in the circuit sorted in ascending order. For qiskit.QuantumCircuit, this order is the same as qiskit.QuantumCircuit.qubits.

Attributes

gates

A sequence of 2-tuple (gate_operand, qubits) representing all gates in the circuit:

Returns

• gate_operand: A ndarray-like tensor object. The modes of the operands are ordered as AB...ab..., where AB... denotes all output modes and ab... denotes all input modes.

• qubits: A list of arrays corresponding to all the qubits and gate tensor operands.

Return type

tuple

qubits

A sequence of all qubits in the circuit.