Skip to content

Statevector Backend

CuStateVecStateBackend()

A pytket Backend using GeneralState to obtain state vectors.

Constructs a new cuStateVec backend object.

Source code in pytket/extensions/custatevec/backends/custatevec_backend.py
def __init__(self) -> None:
    """Constructs a new cuStateVec backend object."""
    super().__init__()

backend_info property

Returns information on the backend.

get_operator_expectation_value(circuit, operator)

Calculate the expectation value of a QubitPauliOperator given a quantum state prepared by a circuit.

This method computes the expectation value of a specified operator with respect to the quantum state generated by the provided state preparation circuit. It leverages cuStateVec for efficient statevector simulation and expectation value computation.

Parameters:

Name Type Description Default
circuit Circuit

The quantum circuit that prepares the desired quantum state.

required
operator QubitPauliOperator

The operator for which the expectation value is to be calculated.

required

Returns:

Type Description
float64

np.float64: The computed expectation value of the operator with respect to

float64

the quantum state.

Source code in pytket/extensions/custatevec/backends/custatevec_backend.py
def get_operator_expectation_value(
    self,
    circuit: Circuit,
    operator: QubitPauliOperator,
) -> np.float64:
    """Calculate the expectation value of a QubitPauliOperator given a quantum state prepared by a circuit.

    This method computes the expectation value of a specified operator with respect
    to the quantum state generated by the provided state preparation circuit. It
    leverages cuStateVec for efficient statevector simulation and expectation value
    computation.

    Args:
        circuit (Circuit): The quantum circuit that prepares the desired
            quantum state.
        operator (QubitPauliOperator): The operator for which the expectation value
            is to be calculated.

    Returns:
        np.float64: The computed expectation value of the operator with respect to
        the quantum state.
    """
    with CuStateVecHandle() as libhandle:
        sv = initial_statevector(
            handle=libhandle,
            n_qubits=circuit.n_qubits,
            sv_type="zero",
            dtype=cudaDataType.CUDA_C_64F,
        )
        run_circuit(libhandle, circuit, sv)
        return compute_expectation(libhandle, sv, operator, circuit)

process_circuit(circuit, n_shots=None, valid_check=True, **kwargs)

Submits circuits to the backend for running.

Source code in pytket/extensions/custatevec/backends/custatevec_backend.py
def process_circuit(
    self,
    circuit: Circuit,
    n_shots: int | None = None,
    valid_check: bool = True,
    **kwargs: KwargTypes,
) -> ResultHandle:
    """Submits circuits to the backend for running."""
    return self.process_circuits(
        [circuit],
        n_shots=n_shots,
        valid_check=valid_check,
        **kwargs,
    )[0]

process_circuits(circuits, n_shots=None, valid_check=True, **kwargs)

Submits circuits to the backend for running.

The results will be stored in the backend's result cache to be retrieved by the corresponding get_ method.

Parameters:

Name Type Description Default
circuits Sequence[Circuit]

List of circuits to be submitted.

required
n_shots int | Sequence[int] | None

Number of shots in case of shot-based calculation. This is unused, since this backend does not support shots.

None
valid_check bool

Whether to check for circuit correctness.

True

Returns:

Type Description
list[ResultHandle]

Results handle objects.

Source code in pytket/extensions/custatevec/backends/custatevec_backend.py
def process_circuits(  # noqa: D417
    self,
    circuits: Sequence[Circuit],
    n_shots: int | Sequence[int] | None = None,  # noqa: ARG002
    valid_check: bool = True,
    **kwargs: KwargTypes,  # noqa: ARG002
) -> list[ResultHandle]:
    """Submits circuits to the backend for running.

    The results will be stored in the backend's result cache to be retrieved by the
    corresponding get_<data> method.

    Args:
        circuits: List of circuits to be submitted.
        n_shots: Number of shots in case of shot-based calculation.
            This is unused, since this backend does not support shots.
        valid_check: Whether to check for circuit correctness.

    Returns:
        Results handle objects.
    """
    if valid_check:
        self._check_all_circuits(circuits, nomeasure_warn=False)

    handle_list = []
    for circuit in circuits:
        with CuStateVecHandle() as libhandle:
            sv = initial_statevector(
                handle=libhandle,
                n_qubits=circuit.n_qubits,
                sv_type="zero",
                dtype=cudaDataType.CUDA_C_64F,
            )
            run_circuit(libhandle, circuit, sv)
        handle = ResultHandle(str(uuid4()))
        # In order to be able to use the BackendResult functionality,
        # we only pass the array of the statevector to BackendResult
        self._cache[handle] = {"result": BackendResult(state=cp.asnumpy(sv.array))}
        handle_list.append(handle)
    return handle_list