in src/braket/circuits/unitary_calculation.py [0:0]
def calculate_unitary(qubit_count: int, instructions: Iterable[Instruction]) -> np.ndarray:
"""
Returns the unitary matrix representation for all the `instructions` with a given
`qubit_count`.
*Note*: The performance of this method degrades with qubit count. It might be slow for
qubit count > 10.
Args:
qubit_count (int): Total number of qubits, enough for all the `instructions`.
instructions (Iterable[Instruction]): The instructions for which the unitary matrix
will be calculated.
Returns:
np.ndarray: A numpy array with shape (2^qubit_count, 2^qubit_count) representing the
`instructions` as a unitary.
Raises:
TypeError: If `instructions` is not composed only of `Gate` instances,
i.e. a circuit with `Noise` operators will raise this error.
"""
unitary = np.eye(2 ** qubit_count, dtype=complex)
un_tensor = np.reshape(unitary, qubit_count * [2, 2])
for instr in instructions:
if not isinstance(instr.operator, Gate):
raise TypeError("Only Gate operators are supported to build the unitary")
matrix = instr.operator.to_matrix()
targets = instr.target
gate_indexes, un_indexes, result_indexes = _einsum_subscripts(targets, qubit_count)
gate_matrix = np.reshape(matrix, len(targets) * [2, 2])
un_tensor = np.einsum(
gate_matrix,
gate_indexes,
un_tensor,
un_indexes,
result_indexes,
dtype=complex,
casting="no",
)
return np.reshape(un_tensor, 2 * [2 ** qubit_count])