mimiqcircuits.operations.gates.customdiagonal

Custom diagonal gate definition.

Classes

GateCustomDiagonal(diagonal)

Gate diagonal in the computational basis, given by its diagonal.

class GateCustomDiagonal(diagonal)[source]

Bases: Gate

Gate diagonal in the computational basis, given by its diagonal.

Same role as GateCustom, but only the \(2^n\) diagonal entries are stored, so a wide diagonal gate costs \(2^n\) numbers instead of \(4^n\). The entries of a numeric diagonal must be phases, abs(d) == 1 within 1e-8; a diagonal with a symbolic entry is not checked.

The entries follow the diagonal of the dense matrix, in the basis |0...0>, ..., |1...1> with the first target qubit as the most significant bit, the same convention as GateCustom.

Parameters:

diagonal (list, tuple, numpy.ndarray or symengine.Matrix) – The \(2^n\) diagonal entries, \(n \geq 1\).

Raises:
  • TypeError – If diagonal is not one of the accepted types.

  • ValueError – If the number of entries is not a power of two greater than one, or a numeric entry does not have modulus one.

Examples

>>> from mimiqcircuits import *
>>> # the diagonal of a CZ gate
>>> g = GateCustomDiagonal([1, 1, 1, -1])
>>> g.num_qubits
2
>>> c = Circuit()
>>> c.push(g, 0, 1)
2-qubit circuit with 1 instruction:
└── CustomDiagonal(...) @ q[0:1]

>>> # the decomposition uses one Z-parity rotation per Walsh term
>>> g.decompose()
2-qubit circuit with 4 instructions:
├── RZ(1.5707963267948966) @ q[1]
├── RZ(1.5707963267948966) @ q[0]
├── RNZ(-1.5707963267948966) @ q[0:1]
└── U(0, 0, 0, 0.7853981633974483) @ q[0]
matrix()[source]

Return the dense diagonal 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 \(2^n \times 2^n\) matrix on each call.

Return type:

symengine.Matrix

diagonal()[source]

Return the diagonal entries as a list.

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

unwrappeddiagonal()[source]

Return the diagonal as a complex128 NumPy array.

Raises:

TypeError – If any entry is still symbolic.

unwrappedmatrix()[source]

Return the dense matrix as a complex128 NumPy array.

Raises:

TypeError – If any entry is still symbolic.

property num_qubits

Number of qubits the gate acts on, \(\log_2\) of the diagonal length.

property parnames

the 1-based indices of the diagonal entries.

Type:

Parameter names

getparams()[source]

Return the stored diagonal entries as a list.

getparam(pn)[source]

Return the diagonal entry with 1-based index pn.

Raises:

IndexError – If pn is outside 1..2**n.

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.

pretty_print()[source]

Return a multi-line string listing the diagonal entries.

evaluate(d)[source]

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

Parameters:

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

Returns:

self if no entry is symbolic, otherwise a new gate built from the substituted entries.

Return type:

GateCustomDiagonal