# How to get the matrix representation of a circuit in Amazon braket?

Say I define a circuit using the amazon-braket-sdk, for example:

braket_circuit = braket.circuits.Circuit()

braket_circuit.h(0)
braket_circuit.h(1)
braket_circuit.h(2)
braket_circuit.rx(0, np.pi / 4)
braket_circuit.ry(1, np.pi / 2)
braket_circuit.rz(2, 3 * np.pi / 4)


Are there any built-in functions that allow you to retrieve the matrix representation of the entire circuit? If all else fails, I know that I can go moment-by-moment, gate-by-gate, and use tensor products to iteratively calculate the circuit's unitary. However, I'm hoping for a one-liner similar to cirq.protocols.unitary or qiskit.quantum_info.Operator.data. Does such a function exist in braket? The braket.circuits.quantum_operator.QuantumOperator class contains a to_matrix method, but it's not clear if or how a braket Circuit can be converted to a QuantumOperation without defining a new, custom QuantumOperation which would of course require its own implementation of the to_matrix method. Thanks for any help in advance!

Up until very recently, braket had no such feature. However, as of v1.7.0 (2021-06-25), unitary representations of circuits can now be calculated using the calculate_unitary function. Using the circuit you provided as an example:

In : from braket.circuits import Circuit
...: from braket.circuits.gate import Gate
...: from braket.circuits.instruction import Instruction
...: from braket.circuits.unitary_calculation import calculate_unitary
...: import numpy as np

In : circuit = Circuit()
...:
...: instructions = [
...:     Instruction(Gate.H(), 0),
...:     Instruction(Gate.H(), 1),
...:     Instruction(Gate.H(), 2),
...:     Instruction(Gate.Rx(np.pi/4), 0),
...:     Instruction(Gate.Ry(np.pi/2), 1),
...:     Instruction(Gate.Rz(3*np.pi/4), 2),
...: ]
...:
...: for inst in instructions:
...:
...: print(circuit)
T  : |0|    1    |

q0 : -H-Rx(0.785)-

q1 : -H-Ry(1.57)--

q2 : -H-Rz(2.36)--

T  : |0|    1    |

In : matrix_rep = calculate_unitary(3, instructions)
...: matrix_rep.shape
Out: (8, 8)