qml.generator¶
- generator(op, format='prefactor')[source]¶
Returns the generator of an operation.
- Parameters
op (Operator or Callable) – A single operator, or a function that applies a single quantum operation.
format (str) – The format to return the generator in. Must be one of
'prefactor'
,'observable'
, or'hamiltonian'
. See below for more details.
- Returns
The returned generator, with format/type dependent on the
format
argument."prefactor"
: Return the generator as(obs, prefactor)
(representing \(G=p \hat{O}\)), where:observable \(\hat{O}\) is one of
Hermitian
,SparseHamiltonian
, or a tensor product of Pauli words.prefactor \(p\) is a float.
"observable"
: Return the generator as a single observable as directly defined byop
. Returned generators may be any type of observable, includingHermitian
,Tensor
,SparseHamiltonian
, orHamiltonian
."hamiltonian"
: Similar to"observable"
, however the returned observable will always be converted intoHamiltonian
regardless of howop
encodes the generator."arithmetic"
: Similar to"hamiltonian"
, however the returned observable will always be converted into an arithmetic operator. The returned generator may be any type, including:SProd
,Prod
,Sum
, or the operator itself.
- Return type
Operator or tuple[Observable, float]
Example
Given an operation,
qml.generator
returns the generator representation:>>> op = qml.CRX(0.6, wires=[0, 1]) >>> qml.generator(op) (Projector([1], wires=[0]) @ X(1), -0.5)
It can also be used in a functional form:
>>> qml.generator(qml.CRX)(0.6, wires=[0, 1]) (Projector([1], wires=[0]) @ X(1), -0.5)
By default,
generator
will return the generator in the format of(obs, prefactor)
, corresponding to \(G=p \hat{O}\), where the observable \(\hat{O}\) will always be given in tensor product form, or as a dense/sparse matrix.By using the
format
argument, the returned generator representation can be altered:>>> op = qml.RX(0.2, wires=0) >>> qml.generator(op, format="prefactor") # output will always be (obs, prefactor) (X(0), -0.5) >>> qml.generator(op, format="hamiltonian") # output will always be a Hamiltonian/LinearCombination -0.5 * X(0) >>> with qml.operation.disable_new_opmath_cm(): ... gen = qml.generator(op, format="hamiltonian")) # legacy Hamiltonian class ... print(gen, type(gen)) (-0.5) [X0] <class 'pennylane.ops.qubit.hamiltonian.Hamiltonian'> >>> qml.generator(qml.PhaseShift(0.1, wires=0), format="observable") # ouput will be a simplified obs where possible Projector([1], wires=[0]) >>> qml.generator(op, format="arithmetic") # output is an instance of `SProd` -0.5 * X(0)