braket.default_simulator.linalg_utils module
- class braket.default_simulator.linalg_utils.QuantumGateDispatcher(n_qubits: int)[source]
Bases:
objectDispatcher for performance-optimized implementations of quantum gates. It automatically selects between small-circuit (NumPy-based) and large-circuit (Numba JIT-compiled) implementations based on the number of qubits in a circuit.
- braket.default_simulator.linalg_utils.multiply_matrix(state: ndarray, matrix: ndarray, targets: tuple[int, ...], controls: tuple[int, ...] | None = (), control_state: tuple[int, ...] | None = (), out: ndarray | None = None, dispatcher: QuantumGateDispatcher | None = None, return_swap_info: bool = False, gate_type: str | None = None) ndarray | tuple[ndarray, bool][source]
Multiplies the given matrix by the given state, applying the matrix on the target qubits, controlling the operation as specified.
- Parameters:
state (np.ndarray) – The state to multiply the matrix by.
matrix (np.ndarray) – The matrix to apply to the state.
targets (tuple[int]) – The qubits to apply the state on.
controls (tuple[int, ...] | None) – The qubits to control the operation on. Default ().
control_state (tuple[int, ...] | None) – A tuple of same length as
controlswith either a 0 or 1 in each index, corresponding to whether to control on the|0⟩or|1⟩state. Default (1,) * len(controls).out (np.ndarray | None) – Preallocated result array to reduce overhead of creating a new array each time.
dispatcher (QuantumGateDispatcher) – Dispatch to optimized functions based on qubit count.
return_swap_info (bool) – For backwards comp. Used to indicate whether the ping-pong buffer swaps should happen.
- Returns:
np.ndarray | tuple[np.ndarray, bool] –
- The state after the matrix has been applied.
When return_swap_info is True, returns a tuple of (state, swap_occurred). When return_swap_info is False, returns just the state array.
- braket.default_simulator.linalg_utils.controlled_matrix(matrix: ndarray, control_state: tuple[int, ...]) ndarray[source]
Returns the controlled form of the given matrix
A controlled matrix is produced by successively taking the direct sum of the matrix \(U_n\) with an equal-rank identity matrix \(I_n\), with regular control (indicated by a control value of 1) taking the direct sum on the left
\[C_1(U_n) := I_n \oplus U_n\]and negative control (indicated by a control value of 0) taking the direct sum on the right
\[C_0(U_n) := U_n \oplus I_n\]The control state is read from left to right, with each control bit doubling the size of the matrix. The output matrix will have rank
2**len(ctrl_state)times that of the input matrix.- Parameters:
matrix (np.ndarray) – The matrix to control
control_state (tuple[int, ...]) – Basis state on which to control the operation. Each appearance of 1 yields a left direct sum, and 0 yields a right direct sum.
- Returns:
np.ndarray – The controlled form of the matrix
- braket.default_simulator.linalg_utils.marginal_probability(probabilities: ndarray, targets: Sequence[int] | None = None) ndarray[source]
Return the marginal probability of the computational basis states.
The marginal probability is obtained by summing the probabilities on the unused qubits. If no targets are specified, then the probability of all basis states is returned.
- Parameters:
probabilities (np.ndarray) – The probability distribution to marginalize.
targets (list[int]) – The qubits of the marginal distribution; if no targets are specified, then the probability of all basis states is returned.
- Returns:
np.ndarray – The marginal probability distribution.
- braket.default_simulator.linalg_utils.partial_trace(density_matrix: ndarray, targets: list[int] | None = None) ndarray[source]
Returns the reduced density matrix for the target qubits.
If no target qubits are supplied, this method returns the trace of the density matrix.
- Parameters:
density_matrix (np.ndarray) – The density matrix to reduce, as a tensor product of qubit states.
targets (list[int] | None) – The qubits of the output reduced density matrix; if no target qubits are supplied, this method returns the trace of the density matrix.
- Returns:
np.ndarray – The partial trace of the density matrix.