Skip to content

Commit

Permalink
CircuitGraph allows the same operation instance to occur multiple tim…
Browse files Browse the repository at this point in the history
…es (#5907)

**Context:**

`CircuitGraph` is from the early queuing era, when we could trust that
every operation in a circuit was unique, since queuing had that
constraint.

This is no longer the case, and can cause errors when the same operation
is repeated multiple times. For example, in
`TrotterProduct.compute_decomposition`.

**Description of the Change:**

The nodes of the `CircuitGraph` are now the indices into the queue.
Indices will be fully unique, even if the same operation occurs multiple
times.

This is accomplished in conjunction with two new private attributes:

* `_nodes_on_wires`: a dictionary mapping a wire to the nodes that are
on that wire
* `_inds_for_objs`: a dictionary mapping a `WrappedObj` instance to all
the indices where that instance occurs. This is a more general successor
to `Operator.queue_idx`. By making this information a private attribute
on `CircuitGraph`, we won't have conflicts if the same operation
instance occurs in multiple different tapes/ graphs.

The private properties `_grid` , `_indices`, and `_operation_graph` are
removed, as they are no longer needed.

The private method `_in_topological_order` is removed, since it was a
private method that nothing used.

`graph.operations_in_order` and `graph.observable_in_order` now just
return `graph.operations` and `graph.observables` respectively, since
those lists are already in order. It was basically "sort this list by
the order the objects come in the list". They already come in the order
that they come in the list?

`_depth` and `max_simultaneous_measurements` are now cached properties.

The method for calculating the depth is also simplified. Now we simply
provide a weight function on the edges to `rx.dag_longest_path_length`.

**Benefits:**

We can use the same instance multiple times in a `CircuitGraph` object.

**Possible Drawbacks:**

`CircuitGraph` no longer sets `Operator.queue_idx`. Maybe someone wanted
that.

The nodes for `CircuitGraph` are now integer indices, rather than
operations. So anyone relying on the individual nodes will see a change
in behavior.

**Related GitHub Issues:**

Fixes #4406 [sc-42719]

---------

Co-authored-by: Astral Cai <[email protected]>
Co-authored-by: Mudit Pandey <[email protected]>
Co-authored-by: Cristian Emiliano Godinez Ramirez <[email protected]>
  • Loading branch information
4 people authored Jul 18, 2024
1 parent e76e301 commit 00cea58
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 310 deletions.
1 change: 0 additions & 1 deletion .github/workflows/core_tests_durations.json
Original file line number Diff line number Diff line change
Expand Up @@ -13182,7 +13182,6 @@
"ops/op_math/test_composite.py::TestConstruction::test_map_wires[True-expected_overlapping_ops1]": 0.001044666045345366,
"ops/op_math/test_composite.py::TestConstruction::test_ndim_params_raises_error": 0.0008577080443501472,
"ops/op_math/test_composite.py::TestConstruction::test_parameters": 0.0009155830484814942,
"ops/op_math/test_composite.py::TestConstruction::test_queue_idx": 0.0008379160426557064,
"ops/op_math/test_composite.py::TestConstruction::test_raise_error_fewer_than_2_operands": 0.0010355000267736614,
"ops/op_math/test_composite.py::TestConstruction::test_tensor_and_hamiltonian_converted": 0.0014357499894686043,
"ops/op_math/test_composite.py::TestMscMethods::test_copy[ops_lst0]": 0.0009615410235710442,
Expand Down
4 changes: 4 additions & 0 deletions doc/development/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Other deprecations
Completed deprecation cycles
----------------------------

* ``queue_idx`` attribute has been removed from the ``Operator``, ``CompositeOp``, and ``SymboliOp`` classes. Instead, the index is now stored as the label of the ``CircuitGraph.graph`` nodes.

- Deprecated in v0.38
- Removed in v0.38

* ``qml.from_qasm`` no longer removes measurements from the QASM code. Use
``measurements=[]`` to remove measurements from the original circuit.
Expand Down
12 changes: 12 additions & 0 deletions doc/releases/changelog-dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@

<h3>Breaking changes 💔</h3>

* The `CircuitGraph.graph` rustworkx graph now stores indices into the circuit as the node labels,
instead of the operator/ measurement itself. This allows the same operator to occur multiple times in
the circuit.
[(#5907)](https://github.com/PennyLaneAI/pennylane/pull/5907)

* `queue_idx` attribute has been removed from the `Operator`, `CompositeOp`, and `SymboliOp` classes.
[(#6005)](https://github.com/PennyLaneAI/pennylane/pull/6005)

* ``qml.from_qasm`` no longer removes measurements from the QASM code. Use
``measurements=[]`` to remove measurements from the original circuit.
[(#5982)](https://github.com/PennyLaneAI/pennylane/pull/5982)
Expand All @@ -81,6 +89,9 @@

<h3>Bug fixes 🐛</h3>

* `CircuitGraph` can now handle circuits with the same operation instance occuring multiple times.
[(#5907)](https://github.com/PennyLaneAI/pennylane/pull/5907)

* `qml.QSVT` is updated to store wire order correctly.
[(#5959)](https://github.com/PennyLaneAI/pennylane/pull/5959)

Expand All @@ -91,6 +102,7 @@
* `qml.AmplitudeEmbedding` has better support for features using low precision integer data types.
[(#5969)](https://github.com/PennyLaneAI/pennylane/pull/5969)


<h3>Contributors ✍️</h3>

This release contains contributions from (in alphabetical order):
Expand Down
Loading

0 comments on commit 00cea58

Please sign in to comment.