cirq.Circuit.to_unitary_matrix

Circuit.to_unitary_matrix(qubit_order: Union[cirq.ops.qubit_order.QubitOrder, Iterable[cirq.ops.raw_types.Qid]] = <cirq.ops.qubit_order.QubitOrder object>, qubits_that_should_be_present: Iterable[cirq.ops.raw_types.Qid] = (), ignore_terminal_measurements: bool = True, dtype: Type[numpy.number] = <class 'numpy.complex128'>) → numpy.ndarray[source]

Converts the circuit into a unitary matrix, if possible.

Parameters:
  • qubit_order – Determines how qubits are ordered when passing matrices into np.kron.
  • qubits_that_should_be_present – Qubits that may or may not appear in operations within the circuit, but that should be included regardless when generating the matrix.
  • ignore_terminal_measurements – When set, measurements at the end of the circuit are ignored instead of causing the method to fail.
  • dtype – The numpy dtype for the returned unitary. Defaults to np.complex128. Specifying np.complex64 will run faster at the cost of precision. dtype must be a complex np.dtype, unless all operations in the circuit have unitary matrices with exclusively real coefficients (e.g. an H + TOFFOLI circuit).
Returns:

A (possibly gigantic) 2d numpy array corresponding to a matrix equivalent to the circuit’s effect on a quantum state.

Raises:
  • ValueError – The circuit contains measurement gates that are not ignored.
  • TypeError – The circuit contains gates that don’t have a known unitary matrix, e.g. gates parameterized by a Symbol.