3

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!

Jay Muntz
  • 147
  • 1
  • 10

2 Answers2

1

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 [1]: 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 [2]: 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: ...: circuit.add_instruction(inst) ...: ...: print(circuit) T : |0| 1 |

q0 : -H-Rx(0.785)-

q1 : -H-Ry(1.57)--

q2 : -H-Rz(2.36)--

T : |0| 1 |

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

ryanhill1
  • 2,493
  • 1
  • 9
  • 37
1

Note that as_unitary has been deprecated because it uses little-endian qubit order. The new, big-endian method is to_unitary.

Cody Wang
  • 1,203
  • 7
  • 13