QuantumTape map_wires equality

Hello! If applicable, put your complete code example down below. Make sure that your code:

  • is 100% self-contained — someone can copy-paste exactly what is here and run it to
    reproduce the behaviour you are observing
  • includes comments

Code to reverse wire ordering using qml.map_wires.

Wires seem to be re-ordered as expected. However, fails equality comparison against regularly constructed tape with reverse wire ordering.

import pennylane as qml

def two_wire_tape(wire1, wire2):
    with qml.tape.QuantumTape() as tape:
        qml.CNOT(wires=[wire1, wire2])
    return tape

wires = [0, 1]
wires_rev = wires[::-1]

tape = two_wire_tape(*wires)
tape_rev_expected = two_wire_tape(*wires_rev)

wire_map = dict(zip(wires, wires_rev))

[tape_rev], _ = qml.map_wires(tape, wire_map)

assert tape_rev.wires == tape_rev_expected.wires  # True
assert tape_rev.operations == tape_rev_expected.operations  # True
assert str(tape_rev) == str(tape_rev_expected)  # True
assert repr(tape_rev) == repr(tape_rev_expected)  # True
assert tape_rev == tape_rev_expected  # False

If you want help with diagnosing an error, please put the full error message below:

assert <QuantumTape:... 0], params=0> == <QuantumTape:... 0], params=0>
E         (pytest_assertion plugin: representation of details failed: /path/to/env/lib/python3.10/site-packages/pennylane/queuing.py:413: KeyError: Wrapped(Hadamard(wires=[1])).
E          Probably an object has a faulty __repr__.)

And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

Name: PennyLane
Version: 0.33.1
Summary: PennyLane is a Python quantum machine learning library by Xanadu Inc.
Home-page: https://github.com/PennyLaneAI/pennylane
License: Apache License 2.0
Location: /Users/ryanhill/anaconda3/envs/sdk310/lib/python3.10/site-packages
Requires: appdirs, autograd, autoray, cachetools, networkx, numpy, pennylane-lightning, requests, rustworkx, scipy, semantic-version, toml, typing-extensions
Required-by: PennyLane-Lightning

Platform info:           macOS-14.1.2-arm64-arm-64bit
Python version:          3.10.13
Numpy version:           1.26.3
Scipy version:           1.11.4
Installed devices:
- default.gaussian (PennyLane-0.33.1)
- default.mixed (PennyLane-0.33.1)
- default.qubit (PennyLane-0.33.1)
- default.qubit.autograd (PennyLane-0.33.1)
- default.qubit.jax (PennyLane-0.33.1)
- default.qubit.legacy (PennyLane-0.33.1)
- default.qubit.tf (PennyLane-0.33.1)
- default.qubit.torch (PennyLane-0.33.1)
- default.qutrit (PennyLane-0.33.1)
- null.qubit (PennyLane-0.33.1)
- lightning.qubit (PennyLane-Lightning-0.33.1)

Thanks for asking @ryanhill1 .

While we have changed the dunder behavior for Operator and MeasurementProcess to depend on the contents instead of the object identity, we have not made that switch for QuantumTape. We could potentially consider doing so in the future.

For now, you can use qml.equal(tape1, tape2) to check equality of two tapes.

1 Like

Thanks @christina. I tried out qml.equal(tape1, tape2) but got:

NotImplementedError: Comparison of <class 'pennylane.tape.tape.QuantumTape'> and <class 'pennylane.tape.tape.QuantumTape'> not implemented

Ah sorry. That got added to master in November. But it will be in the next release coming out later today/ tomorrow!