qp.compiler.active

active()[source]

Check whether the caller is inside a qjit() evaluation context.

This helper function may be used during implementation to allow differing logic for circuits or operations that are just-in-time compiled versus those that are not.

Returns:

True if the caller is inside a QJIT evaluation context

Return type:

bool

Example

For example, you can use this method in your hybrid program to execute it conditionally whether called inside qjit() or not.

dev = qp.device("lightning.qubit", wires=2)

@qp.qnode(dev)
def circuit(phi, theta):
    if qp.compiler.active():
        qp.RX(phi, wires=0)
    qp.CNOT(wires=[0, 1])
    qp.PhaseShift(theta, wires=0)
    return qp.expval(qp.Z(0))
>>> circuit(np.pi, np.pi / 2)
1.0
>>> qp.qjit(circuit)(np.pi, np.pi / 2)
-1.0