mimiqcircuits.operations.gates.custom

Custom gate definition.

Classes

GateCustom(matrix)

Custom gate defined by a unitary matrix.

class GateCustom(matrix)[source]

Bases: Gate

Custom gate defined by a unitary matrix.

The matrix must be square with \(2^n\) rows, \(n \geq 1\); the gate then acts on \(n\) qubits. Qubits are ordered big-endian: the first target qubit is the most significant bit of the row index. Numeric matrices are checked for unitarity with an absolute tolerance of 1e-8. The check is skipped when an entry is a plain symbol, so a symbolic matrix can be filled in later with evaluate().

Parameters:

matrix (numpy.ndarray or symengine.Matrix) – The unitary matrix.

Raises:
  • TypeError – If matrix is neither a NumPy array nor a SymEngine matrix.

  • ValueError – If the matrix is not square, not unitary, or its size is not a power of two greater than one.

Examples

>>> from mimiqcircuits import *
>>> import numpy as np
>>> # a controlled-Y written out as a 4x4 matrix
>>> matrix = np.array([[1, 0, 0, 0],[0, 1, 0, 0],[0, 0, 0, -1j],[0, 0, 1j, 0]])
>>> g = GateCustom(matrix)
>>> g.num_qubits
2
>>> c = Circuit()
>>> c.push(g, 0, 1)
2-qubit circuit with 1 instruction:
└── Custom(...) @ q[0:1]

>>> # a single qubit custom gate decomposes into one GateU
>>> GateCustom(np.array([[0, 1], [1, 0]])).decompose()
1-qubit circuit with 1 instruction:
└── U(3.141592653589793, 0.0, -3.141592653589793, 0.0) @ q[0]
matrix()[source]

Return the gate matrix as a SymEngine matrix.

Entries that can be converted to a complex number are returned as complex numbers; symbolic entries are kept as they are.

Returns:

A new matrix on each call.

Return type:

symengine.Matrix

property num_qubits

Number of qubits the gate acts on, \(\log_2\) of the matrix size.

property parnames

the 1-based linear indices of the matrix entries.

Type:

Parameter names

getparams()[source]

Return the matrix entries as a list, in column-major order.

getparam(pn)[source]

Return the matrix entry with 1-based column-major index pn.

Raises:

IndexError – If pn is outside 1..rows*cols.

inverse()[source]

Return the inverse of the gate.

Gates with a known inverse return it directly (for example GateS().inverse() is GateSDG()); otherwise the gate is wrapped in Inverse.

static is_unitary(matrix, tol=1e-08)[source]

Return whether \(U U^\dagger\) equals the identity within tol.

Parameters:
  • matrix (numpy.ndarray or symengine.Matrix) – The matrix to check.

  • tol (float) – Absolute tolerance on each entry. Defaults to 1e-8.

Returns:

True if the matrix is unitary.

Return type:

bool

pretty_print()[source]

Return a multi-line string showing the matrix row by row.

evaluate(d)[source]

Substitute symbols in the matrix entries and return a new gate.

Parameters:

d (dict) – Map from symbols to the values to substitute.

Returns:

self if the matrix has no symbols, otherwise a new gate built from the substituted matrix (checked for unitarity again).

Return type:

GateCustom

unwrappedmatrix()[source]

Return the matrix as a complex128 NumPy array.

Raises:

TypeError – If an entry is still symbolic.