Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly compare Controlled operators with differing control wire order #4944

Merged
merged 6 commits into from Dec 13, 2023

Conversation

lillian542
Copy link
Contributor

Context:
As long as the mapping of control wires to control values is the same, the order of control wires doesn't matter when comparing two Controlled operators, but the current implementation of qml.equal doesn't consider this. This currently returns False, because the wires differ in order:

base1 = qml.PauliX(wires=0)
base2 = qml.PauliX(wires=0)

op1 = qml.ops.op_math.Controlled(base1, control_wires=[1, 2])
op2 = qml.ops.op_math.Controlled(base2, control_wires=[2, 1])
print(qml.equal(op1, op2))

Description of the Change:
Instead of comparing the operator wires and the control values, we compare the work_wires of the two operators, and we compare dictionaries of {wire1: value1, wire2: value2...}.

Benefits:
The comparison of operators via qml.equal is more complete.

Related GitHub Issues:
#4842

@lillian542 lillian542 linked an issue Dec 12, 2023 that may be closed by this pull request
1 task
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

@lillian542
Copy link
Contributor Author

[sc-50266]

@lillian542 lillian542 requested a review from a team December 12, 2023 20:57
Copy link

codecov bot commented Dec 12, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (18608ef) 99.66% compared to head (a7008c5) 99.66%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4944      +/-   ##
==========================================
- Coverage   99.66%   99.66%   -0.01%     
==========================================
  Files         388      388              
  Lines       35379    35111     -268     
==========================================
- Hits        35262    34993     -269     
- Misses        117      118       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@timmysilv timmysilv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! 🏆 🎉

@lillian542 lillian542 enabled auto-merge (squash) December 13, 2023 17:05
@lillian542 lillian542 merged commit f435f87 into master Dec 13, 2023
35 checks passed
@lillian542 lillian542 deleted the controlled_equal_update branch December 13, 2023 20:06
mudit2812 pushed a commit that referenced this pull request Dec 15, 2023
…der (#4944)

**Context:**
As long as the mapping of control wires to control values is the same,
the order of control wires doesn't matter when comparing two Controlled
operators, but the current implementation of `qml.equal` doesn't
consider this. This currently returns False, because the wires differ in
order:
```
base1 = qml.PauliX(wires=0)
base2 = qml.PauliX(wires=0)

op1 = qml.ops.op_math.Controlled(base1, control_wires=[1, 2])
op2 = qml.ops.op_math.Controlled(base2, control_wires=[2, 1])
print(qml.equal(op1, op2))
```

**Description of the Change:**
Instead of comparing the operator wires and the control values, we
compare the `work_wires` of the two operators, and we compare
dictionaries of `{wire1: value1, wire2: value2...}`.

**Benefits:**
The comparison of operators via `qml.equal` is more complete.

**Related GitHub Issues:**
#4842
mudit2812 pushed a commit that referenced this pull request Jan 19, 2024
…der (#4944)

**Context:**
As long as the mapping of control wires to control values is the same,
the order of control wires doesn't matter when comparing two Controlled
operators, but the current implementation of `qml.equal` doesn't
consider this. This currently returns False, because the wires differ in
order:
```
base1 = qml.PauliX(wires=0)
base2 = qml.PauliX(wires=0)

op1 = qml.ops.op_math.Controlled(base1, control_wires=[1, 2])
op2 = qml.ops.op_math.Controlled(base2, control_wires=[2, 1])
print(qml.equal(op1, op2))
```

**Description of the Change:**
Instead of comparing the operator wires and the control values, we
compare the `work_wires` of the two operators, and we compare
dictionaries of `{wire1: value1, wire2: value2...}`.

**Benefits:**
The comparison of operators via `qml.equal` is more complete.

**Related GitHub Issues:**
#4842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Equal does not work with symmetry of control wires
3 participants