in src/braket/default_simulator/linalg_utils.py [0:0]
def multiply_matrix(state: np.ndarray, matrix: np.ndarray, targets: Tuple[int, ...]) -> np.ndarray:
"""Multiplies the given matrix by the given state, applying the matrix on the target qubits.
Args:
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.
Returns:
np.ndarray: The state after the matrix has been applied.
"""
gate_matrix = np.reshape(matrix, [2] * len(targets) * 2)
axes = (
np.arange(len(targets), 2 * len(targets)),
targets,
)
product = np.tensordot(gate_matrix, state, axes=axes)
# Axes given in `operation.targets` are in the first positions.
unused_idxs = [idx for idx in range(len(state.shape)) if idx not in targets]
permutation = list(targets) + unused_idxs
# Invert the permutation to put the indices in the correct place
inverse_permutation = np.argsort(permutation)
return np.transpose(product, inverse_permutation)