qml.equal¶
- equal(op1, op2, check_interface=True, check_trainability=True, rtol=1e-05, atol=1e-09)[source]¶
Function for determining operator, measurement, and tape equality.
Warning
The
qml.equal
function is based on a comparison of the types and attributes of the measurements or operators, not their mathematical representations. While mathematical 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 interfaces and trainability of data within some observables including
Tensor
,Hamiltonian
,Prod
,Sum
are sometimes ignored, regardless of what the user specifies forcheck_interface
andcheck_trainability
.- Parameters
op1 (Operator or MeasurementProcess or QuantumTape) – First object to compare
op2 (Operator or MeasurementProcess or QuantumTape) – Second object to compare
check_interface (bool, optional) – Whether to compare interfaces. Default:
True
.check_trainability (bool, optional) – Whether to compare trainability status. Default:
True
.rtol (float, optional) – Relative tolerance for parameters.
atol (float, optional) – Absolute tolerance for parameters.
- Returns
True
if the operators, measurement processes, or tapes 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.X(0) @ qml.Y(1) >>> T2 = qml.Y(1) @ qml.X(0) >>> T3 = qml.X(1) @ qml.Y(0) >>> qml.equal(T1, T2), qml.equal(T1, T3) (True, False)
>>> T = qml.X(0) @ qml.Y(1) >>> H = qml.Hamiltonian([1], [qml.X(0) @ qml.Y(1)]) >>> qml.equal(T, H) True
>>> H1 = qml.Hamiltonian([0.5, 0.5], [qml.Z(0) @ qml.Y(1), qml.Y(1) @ qml.Z(0) @ qml.Identity("a")]) >>> H2 = qml.Hamiltonian([1], [qml.Z(0) @ qml.Y(1)]) >>> H3 = qml.Hamiltonian([2], [qml.Z(0) @ qml.Y(1)]) >>> qml.equal(H1, H2), qml.equal(H1, H3) (True, False)
>>> qml.equal(qml.expval(qml.X(0)), qml.expval(qml.X(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 >>> tape1 = qml.tape.QuantumScript([qml.RX(1.2, wires=0)], [qml.expval(qml.Z(0))]) >>> tape2 = qml.tape.QuantumScript([qml.RX(1.2 + 1e-6, wires=0)], [qml.expval(qml.Z(0))]) >>> qml.equal(tape1, tape2, tol=0, atol=1e-7) False >>> qml.equal(tape1, tape2, tol=0, atol=1e-5) True
Usage Details
You can use the optional arguments to get more specific results:
>>> 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