qp.decomposition.gate_set.GateSet

class GateSet(gate_set, name='')[source]

Bases: Mapping

Stores the target gate set of a decomposition pass.

Parameters:
  • gate_set (Iterable | Mapping) – the contents

  • name (str) – a shorthand to use in the str and repr

While the decompose transform can accept any iterable for it’s gate_set argument, the GateSet class provides some helpful tools. This includes a name argument for improved inspection and condensed reprs, immutability for improved protection when used as a global variable, and conversion between class and string based representations of operators.

We can create a gateset using both Operator subclasses or strings, and use both classes and strings to check inclusion in the gateset

>>> from pennylane.decomposition import GateSet
>>> gateset = GateSet({"X", qp.RX, "Adjoint(RX)"})
>>> gateset
GateSet({Adjoint(RX), PauliX, RX})
>>> qp.X in gateset
True
>>> "RX" in gateset
True

We can also provide a name for improved inspection.

>>> gateset_name = GateSet({qp.RX, qp.RY, qp.RZ}, name="Rotations")
>>> print(gateset_name)
Rotations
>>> qp.decompose(gate_set=gateset_name)
<decompose(gate_set=Rotations)>

Gate sets can be combined with |:

>>> gateset | {qp.RX, qp.RY, qp.RZ}
GateSet({Adjoint(RX), PauliX, RX, RY, RZ})

Items can be removed with -:

>>> gateset - {qp.RX, qp.RY}
GateSet({Adjoint(RX), PauliX})
>>> gateset - qp.RX
GateSet({Adjoint(RX), PauliX})
>>> gateset - "RX"
GateSet({Adjoint(RX), PauliX})

Weights can also be provided for use in calculating costs and choosing optimal decompositions:

>>> GateSet({qp.I: 0, qp.RX: 1, qp.CNOT: 3})
GateSet({Identity=0, RX, CNOT=3})

If not provided, weights default to 1:

>>> dict(gateset)
{'Adjoint(RX)': 1.0, 'PauliX': 1.0, 'RX': 1.0}

get(k[,d])

items()

keys()

values()

get(k[, d]) D[k] if k in D, else d.  d defaults to None.[source]
items() a set-like object providing a view on D's items[source]
keys() a set-like object providing a view on D's keys[source]
values() an object providing a view on D's values[source]