qml.equal

equal(op1: Union[pennylane.operation.Operator, pennylane.measurements.measurements.MeasurementProcess], op2: Union[pennylane.operation.Operator, pennylane.measurements.measurements.MeasurementProcess], 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 as Tensor and Hamiltonian, are supported, mathematically equivalent operators defined via different classes may return False when compared via qml.equal.

To be more thorough would require the matrix forms to be calculated, which may drastically increase runtime.

Warning

The kwargs check_interface and check_trainability can only be set when comparing Operation objects. Comparisons of MeasurementProcess or Observable objects will use the default value of True for both, regardless of what the user specifies when calling the function. For subclasses of SymbolicOp or CompositeOp with an Operation 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 comparing MeasurementProcess, Hamiltonian or Tensor objects.

  • check_trainability (bool, optional) – Whether to compare trainability status. Default: True. Not used for comparing MeasurementProcess, Hamiltonian or Tensor objects.

  • rtol (float, optional) – Relative tolerance for parameters. Not used for comparing MeasurementProcess, Hamiltonian or Tensor objects.

  • atol (float, optional) – Absolute tolerance for parameters. Not used for comparing MeasurementProcess, Hamiltonian or Tensor objects.

Returns

True if the operators or measurement processes are equal, else False

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

You can use the optional arguments to get more specific results. Additionally, they are applied when comparing the base of SymbolicOp and CompositeOp operators such as Controlled, Pow, SProd, Prod, etc., if the base is an Operation. These arguments are, however, not used for comparing MeasurementProcess, Hamiltonian or Tensor 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

Contents

Using PennyLane

Development

API

Internals