qml.equal¶
-
equal
(op1, op2, check_interface=True, check_trainability=True, rtol=1e-05, atol=1e-09)[source]¶ Function for determining operator or measurement equality.
Warning
The
qml.equal
function is based on a comparison of the type and attributes of the measurement or operator, not a mathematical representation. While comparisons between some classes, such asTensor
andHamiltonian
, are supported, mathematically equivalent operators defined via different classes may return False when compared viaqml.equal
.To be more thorough would require the matrix forms to be calculated, which may drastically increase runtime.
Warning
The kwargs
check_interface
andcheck_trainability
can only be set when comparingOperation
objects. Comparisons ofMeasurementProcess
orObservable
objects will use the default value ofTrue
for both, regardless of what the user specifies when calling the function. For subclasses ofSymbolicOp
orCompositeOp
with anOperation
as a base, the kwargs will be applied to the base comparison.- Parameters
op1 (Operator or MeasurementProcess) – First object to compare
op2 (Operator or MeasurementProcess) – Second object to compare
check_interface (bool, optional) – Whether to compare interfaces. Default:
True
. Not used for comparingMeasurementProcess
,Hamiltonian
orTensor
objects.check_trainability (bool, optional) – Whether to compare trainability status. Default:
True
. Not used for comparingMeasurementProcess
,Hamiltonian
orTensor
objects.rtol (float, optional) – Relative tolerance for parameters. Not used for comparing
MeasurementProcess
,Hamiltonian
orTensor
objects.atol (float, optional) – Absolute tolerance for parameters. Not used for comparing
MeasurementProcess
,Hamiltonian
orTensor
objects.
- Returns
True
if the operators or measurement processes are equal, elseFalse
- Return type
bool
Example
Given two operators or measurement processes,
qml.equal
determines their equality.>>> op1 = qml.RX(np.array(.12), wires=0) >>> op2 = qml.RY(np.array(1.23), wires=0) >>> qml.equal(op1, op1), qml.equal(op1, op2) (True, False)
>>> T1 = qml.PauliX(0) @ qml.PauliY(1) >>> T2 = qml.PauliY(1) @ qml.PauliX(0) >>> T3 = qml.PauliX(1) @ qml.PauliY(0) >>> qml.equal(T1, T2), qml.equal(T1, T3) (True, False)
>>> T = qml.PauliX(0) @ qml.PauliY(1) >>> H = qml.Hamiltonian([1], [qml.PauliX(0) @ qml.PauliY(1)]) >>> qml.equal(T, H) True
>>> H1 = qml.Hamiltonian([0.5, 0.5], [qml.PauliZ(0) @ qml.PauliY(1), qml.PauliY(1) @ qml.PauliZ(0) @ qml.Identity("a")]) >>> H2 = qml.Hamiltonian([1], [qml.PauliZ(0) @ qml.PauliY(1)]) >>> H3 = qml.Hamiltonian([2], [qml.PauliZ(0) @ qml.PauliY(1)]) >>> qml.equal(H1, H2), qml.equal(H1, H3) (True, False)
>>> qml.equal(qml.expval(qml.PauliX(0)), qml.expval(qml.PauliX(0))) True >>> qml.equal(qml.probs(wires=(0,1)), qml.probs(wires=(1,2))) False >>> qml.equal(qml.classical_shadow(wires=[0,1]), qml.classical_shadow(wires=[0,1])) True
Usage Details
You can use the optional arguments to get more specific results. Additionally, they are applied when comparing the base of
SymbolicOp
andCompositeOp
operators such asControlled
,Pow
,SProd
,Prod
, etc., if the base is anOperation
. These arguments are, however, not used for comparingMeasurementProcess
,Hamiltonian
orTensor
objects.Consider the following comparisons:
>>> op1 = qml.RX(torch.tensor(1.2), wires=0) >>> op2 = qml.RX(jax.numpy.array(1.2), wires=0) >>> qml.equal(op1, op2) False
>>> qml.equal(op1, op2, check_interface=False, check_trainability=False) True
>>> op3 = qml.RX(np.array(1.2, requires_grad=True), wires=0) >>> op4 = qml.RX(np.array(1.2, requires_grad=False), wires=0) >>> qml.equal(op3, op4) False
>>> qml.equal(op3, op4, check_trainability=False) True
>>> qml.equal(Controlled(op3, control_wires=1), Controlled(op4, control_wires=1)) False
>>> qml.equal(Controlled(op3, control_wires=1), Controlled(op4, control_wires=1), check_trainability=False) True