qibotn package#

Subpackages#

Submodules#

qibotn.circuit_convertor module#

class qibotn.circuit_convertor.QiboCircuitToEinsum(circuit, dtype='complex128')#

Bases: object

Convert a circuit to a Tensor Network (TN) representation.

The circuit is first processed to an intermediate form by grouping each gate matrix with its corresponding qubit it is acting on to a list. It is then converted to an equivalent TN expression through the class function state_vector_operands() following the Einstein summation convention in the interleave format.

See document for detail of the format: https://docs.nvidia.com/cuda/cuquantum/python/api/generated/cuquantum.contract.html

The output is to be used by cuQuantum’s contract() for computation of the state vectors of the circuit.

expectation_operands(pauli_string)#

Create the operands for pauli string expectation computation in the interleave format.

Parameters:

pauli_string – A string representating the list of pauli gates.

Returns:

Operands for the contraction in the interleave format.

get_pauli_gates(pauli_map, dtype='complex128', backend=cupy)#

Populate the gates for all pauli operators.

Parameters:
  • pauli_map – A dictionary mapping qubits to pauli operators.

  • dtype – Data type for the tensor operands.

  • backend – The package the tensor operands belong to.

Returns:

A sequence of pauli gates.

init_basis_map(backend, dtype)#

Initialize the basis map for the quantum circuit.

This method initializes a basis map for the quantum circuit, which maps binary strings representing qubit states to their corresponding quantum state vectors.

Parameters:
  • backend (object) – The backend object providing the array conversion method.

  • dtype (object) – The data type for the quantum state vectors.

init_intermediate_circuit(circuit)#

Initialize the intermediate circuit representation.

This method initializes the intermediate circuit representation by extracting gate matrices and qubit IDs from the given quantum circuit.

Parameters:

circuit (object) – The quantum circuit object.

init_inverse_circuit(circuit)#

Initialize the inverse circuit representation.

This method initializes the inverse circuit representation by extracting gate matrices and qubit IDs from the given quantum circuit.

Parameters:

circuit (object) – The quantum circuit object.

op_shape_from_qubits(nqubits)#

Modify tensor to cuQuantum shape.

Parameters:

nqubits (int) – The number of qubits in quantum circuit.

Returns:

(qubit_states,input_output) * nqubits

state_vector_operands()#

Create the operands for dense vector computation in the interleave format.

Returns:

Operands for the contraction in the interleave format.

qibotn.circuit_to_mps module#

class qibotn.circuit_to_mps.QiboCircuitToMPS(circ_qibo, gate_algo, dtype='complex128', rand_seed=0)#

Bases: object

A helper class to convert Qibo circuit to MPS.

Parameters:
  • circ_qibo – The quantum circuit object.

  • gate_algo (dict) – Dictionary for SVD and QR settings.

  • datatype (str) – Either single (“complex64”) or double (complex128) precision.

  • rand_seed (int) – Seed for random number generator.

qibotn.eval module#

qibotn.eval.dense_vector_mps(qibo_circ, gate_algo, datatype)#

Convert qibo circuit to matrix product state (MPS) format and perform contraction to dense vector.

Parameters:
  • qibo_circ – The quantum circuit object.

  • gate_algo (dict) – Dictionary for SVD and QR settings.

  • datatype (str) – Either single (“complex64”) or double (complex128) precision.

Returns:

Dense vector of quantum circuit.

qibotn.eval.dense_vector_tn(qibo_circ, datatype)#

Convert qibo circuit to tensornet (TN) format and perform contraction to dense vector.

Parameters:
  • qibo_circ – The quantum circuit object.

  • datatype (str) – Either single (“complex64”) or double (complex128) precision.

Returns:

Dense vector of quantum circuit.

qibotn.eval.dense_vector_tn_MPI(qibo_circ, datatype, n_samples=8)#

Convert qibo circuit to tensornet (TN) format and perform contraction using multi node and multi GPU through MPI.

The conversion is performed by QiboCircuitToEinsum(), after which it goes through 2 steps: pathfinder and execution. The pathfinder looks at user defined number of samples (n_samples) iteratively to select the least costly contraction path. This is sped up with multi thread. After pathfinding the optimal path is used in the actual contraction to give a dense vector representation of the TN.

Parameters:
  • qibo_circ – The quantum circuit object.

  • datatype (str) – Either single (“complex64”) or double (complex128) precision.

  • n_samples (int) – Number of samples for pathfinding.

Returns:

Dense vector of quantum circuit.

qibotn.eval.dense_vector_tn_nccl(qibo_circ, datatype, n_samples=8)#

Convert qibo circuit to tensornet (TN) format and perform contraction using multi node and multi GPU through NCCL.

The conversion is performed by QiboCircuitToEinsum(), after which it goes through 2 steps: pathfinder and execution. The pathfinder looks at user defined number of samples (n_samples) iteratively to select the least costly contraction path. This is sped up with multi thread. After pathfinding the optimal path is used in the actual contraction to give a dense vector representation of the TN.

Parameters:
  • qibo_circ – The quantum circuit object.

  • datatype (str) – Either single (“complex64”) or double (complex128) precision.

  • n_samples (int) – Number of samples for pathfinding.

Returns:

Dense vector of quantum circuit.

qibotn.eval.expectation_pauli_tn(qibo_circ, datatype, pauli_string_pattern)#

Convert qibo circuit to tensornet (TN) format and perform contraction to expectation of given Pauli string.

Parameters:
  • qibo_circ – The quantum circuit object.

  • datatype (str) – Either single (“complex64”) or double (complex128) precision.

  • pauli_string_pattern (str) – pauli string pattern.

Returns:

Expectation of quantum circuit due to pauli string.

qibotn.eval.expectation_pauli_tn_MPI(qibo_circ, datatype, pauli_string_pattern, n_samples=8)#

Convert qibo circuit to tensornet (TN) format and perform contraction to expectation of given Pauli string using multi node and multi GPU through MPI.

The conversion is performed by QiboCircuitToEinsum(), after which it goes through 2 steps: pathfinder and execution. The pauli_string_pattern is used to generate the pauli string corresponding to the number of qubits of the system. The pathfinder looks at user defined number of samples (n_samples) iteratively to select the least costly contraction path. This is sped up with multi thread. After pathfinding the optimal path is used in the actual contraction to give an expectation value.

Parameters:
  • qibo_circ – The quantum circuit object.

  • datatype (str) – Either single (“complex64”) or double (complex128) precision.

  • pauli_string_pattern (str) – pauli string pattern.

  • n_samples (int) – Number of samples for pathfinding.

Returns:

Expectation of quantum circuit due to pauli string.

qibotn.eval.expectation_pauli_tn_nccl(qibo_circ, datatype, pauli_string_pattern, n_samples=8)#

Convert qibo circuit to tensornet (TN) format and perform contraction to expectation of given Pauli string using multi node and multi GPU through NCCL.

The conversion is performed by QiboCircuitToEinsum(), after which it goes through 2 steps: pathfinder and execution. The pauli_string_pattern is used to generate the pauli string corresponding to the number of qubits of the system. The pathfinder looks at user defined number of samples (n_samples) iteratively to select the least costly contraction path. This is sped up with multi thread. After pathfinding the optimal path is used in the actual contraction to give an expectation value.

Parameters:
  • qibo_circ – The quantum circuit object.

  • datatype (str) – Either single (“complex64”) or double (complex128) precision.

  • pauli_string_pattern (str) – pauli string pattern.

  • n_samples (int) – Number of samples for pathfinding.

Returns:

Expectation of quantum circuit due to pauli string.

qibotn.eval.pauli_string_gen(nqubits, pauli_string_pattern)#

Used internally to generate the string based on given pattern and number of qubit.

Parameters:
  • nqubits (int) – Number of qubits of Quantum Circuit

  • pauli_string_pattern (str) – Strings representing sequence of pauli gates.

Returns:

String representation of the actual pauli string from the pattern.

Example: pattern: “XZ”, number of qubit: 7, output = XZXZXZX

qibotn.eval_qu module#

qibotn.eval_qu.dense_vector_tn_qu(qasm: str, initial_state, mps_opts, backend='numpy')#

Evaluate circuit in QASM format with Quimb.

Parameters:
  • qasm (str) – QASM program.

  • initial_state (list) – Initial state in the dense vector form. If None the default |00...0> state is used.

  • mps_opts (dict) – Parameters to tune the gate_opts for mps settings in class quimb.tensor.circuit.CircuitMPS.

  • backend (str) – Backend to perform the contraction with, e.g. numpy, cupy, jax. Passed to opt_einsum.

Returns:

Amplitudes of final state after the simulation of the circuit.

Return type:

list

qibotn.eval_qu.init_state_tn(nqubits, init_state_sv)#

Create a matrix product state directly from a dense vector.

Parameters:
  • nqubits (int) – Total number of qubits in the circuit.

  • init_state_sv (list) – Initial state in the dense vector form.

Returns:

Matrix product state representation of the dense vector.

Return type:

list

qibotn.mps_contraction_helper module#

class qibotn.mps_contraction_helper.MPSContractionHelper(num_qubits)#

Bases: object

A helper class to compute various quantities for a given MPS.

Interleaved format is used to construct the input args for cuquantum.contract.

Reference: https://github.com/NVIDIA/cuQuantum/blob/main/python/samples/cutensornet/tn_algorithms/mps_algorithms.ipynb

The following compute quantities are supported:

  • the norm of the MPS.

  • the equivalent state vector from the MPS.

  • the expectation value for a given operator.

  • the equivalent state vector after multiplying an MPO to an MPS.

Parameters:

num_qubits – The number of qubits for the MPS.

contract_expectation(mps_tensors, operator, qubits, options=None, normalize=False)#

Contract the corresponding tensor network to form the expectation of the MPS.

Parameters:
  • mps_tensors – A list of rank-3 ndarray-like tensor objects. The indices of the ith tensor are expected to be bonding index to the i-1 tensor, the physical mode, and then the bonding index to the i+1th tensor.

  • operator – A ndarray-like tensor object. The modes of the operator are expected to be output qubits followed by input qubits, e.g, A, B, a, b where a, b denotes the inputs and A, B’ denotes the outputs.

  • qubits – A sequence of integers specifying the qubits that the operator is acting on.

  • options – Specify the contract and decompose options.

  • normalize – Whether to scale the expectation value by the normalization factor.

Returns:

An ndarray-like object as the state vector.

contract_norm(mps_tensors, options=None)#

Contract the corresponding tensor network to form the norm of the MPS.

Parameters:
  • mps_tensors – A list of rank-3 ndarray-like tensor objects. The indices of the ith tensor are expected to be bonding index to the i-1 tensor, the physical mode, and then the bonding index to the i+1th tensor.

  • options – Specify the contract and decompose options.

Returns:

The norm of the MPS.

contract_state_vector(mps_tensors, options=None)#

Contract the corresponding tensor network to form the state vector representation of the MPS.

Parameters:
  • mps_tensors – A list of rank-3 ndarray-like tensor objects. The indices of the ith tensor are expected to be bonding index to the i-1 tensor, the physical mode, and then the bonding index to the i+1th tensor.

  • options – Specify the contract and decompose options.

Returns:

An ndarray-like object as the state vector.

qibotn.mps_utils module#

qibotn.mps_utils.apply_gate(mps_tensors, gate, qubits, **kwargs)#

Apply the gate operand to the MPS tensors in-place.

# Reference: https://github.com/NVIDIA/cuQuantum/blob/main/python/samples/cutensornet/tn_algorithms/mps_algorithms.ipynb

Parameters:
  • mps_tensors – A list of rank-3 ndarray-like tensor objects. The indices of the ith tensor are expected to be the bonding index to the i-1 tensor, the physical mode, and then the bonding index to the i+1th tensor.

  • gate – A ndarray-like tensor object representing the gate operand. The modes of the gate is expected to be output qubits followed by input qubits, e.g, A, B, a, b where a, b denotes the inputs and A, B denotes the outputs.

  • qubits – A sequence of integers denoting the qubits that the gate is applied onto.

  • algorithm – The contract and decompose algorithm to use for gate application. Can be either a dict or a ContractDecomposeAlgorithm.

  • options – Specify the contract and decompose options.

Returns:

The updated MPS tensors.

qibotn.mps_utils.initial(num_qubits, dtype)#

Generate the MPS with an initial state of \(\ket{00...00}\)

Parameters:
  • num_qubits – Number of qubits in the Quantum Circuit.

  • dtype – Either single (“complex64”) or double (complex128) precision.

Returns:

The initial MPS tensors.

qibotn.mps_utils.mps_site_right_swap(mps_tensors, i, **kwargs)#

Perform the swap operation between the ith and i+1th MPS tensors.

Parameters:
  • mps_tensors – Tensors representing MPS

  • i (int) – index of the tensor to swap

Returns:

The updated MPS tensors.