PennyLaneAI / PennyLaneAI/catalyst

[BUG] Incorrect output pytree when using qml.counts() in specific output patterns

Open
#1,016 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
234
Forks
84
Avg merge
2d 15h
Merged PRs (30d)
66

Description

Context

When using qml.counts() in the output of a quantum circuit with qjit, the output pytree is modified to replace the output pytree element related to qml.counts with tree_structure(("keys", "counts")). However this transformation is buggy and while it works for simple cases, it incorrectly transforms more complex patterns.

An example that works fine:

dev = qml.device("lightning.qubit", wires=1, shots=20)
@qjit
@qml.qnode(dev)
def circuit(x):
    qml.RX(x, wires=0)
    return {"1":  qml.counts()}

result = circuit(0.5)
_, result_tree = tree_flatten(result)
print(result_tree)

The result is as expected:

PyTreeDef({'1': (*, *)})

In the following example, there are two patterns that result in the wrong output pytree:

dev = qml.device("lightning.qubit", wires=1, shots=20)
@qjit
@qml.qnode(dev)
def circuit(x):
    qml.RX(x, wires=0)
    return {"1": qml.counts()}, {"2": qml.expval(qml.Z(0))}

result = circuit(0.5)
_, result_tree = tree_flatten(result)
print(result_tree)

results in:

PyTreeDef(((*, *), {'2': *}))

instead of the expected pytree of:

PyTreeDef(({'1': (*, *)}, {'2': *}))
dev = qml.device("lightning.qubit", wires=1, shots=20)
@qjit
@qml.qnode(dev)
def circuit(x):
    qml.RX(x, wires=0)
    return [{"1": qml.expval(qml.Z(0))}, {"2": qml.counts()}], {"3": qml.expval(qml.Z(0))}

result = circuit(0.5)
_, result_tree = tree_flatten(result)
print(result_tree)

results in:

PyTreeDef(([{'1': *}, {'2': *}], (*, *)))

while the expected pytree is:

PyTreeDef(([{'1': *}, {'2': (*, *)}], {'3': *}))

A possible solution would update trace_quantum_measurements(), which is where the output pytree is modified. You could write a function replace_child_tree(tree, i, subtree) which receives a pytree and would replace the ith node of the tree that is visited in a DFS of subtree.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in trace_quantum_measurements(), which the issue identifies as the place where the output pytree is modified, and reproduce both qjit/qml.counts() examples. Compare the actual and expected PyTreeDef outputs; the work is done when both complex patterns preserve the nested dictionaries and counts key/count pairs shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.