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

Add Rust representation for most controlled gates #12659

Merged
merged 25 commits into from
Jul 8, 2024

Conversation

ElePT
Copy link
Contributor

@ElePT ElePT commented Jun 25, 2024

Summary

This PR adds Rust implementations for most controlled gates left in #12566.

  • CUGate
  • CU1Gate
  • CU3Gate
  • C3XGate
  • C3SXGate
  • CCZGate
  • RCCXGate
  • RC3XGate

This is done through a newly implemented make_n_controlled_gate macro (which is currently limited to adding controls to a single-qubit gate).

Details and comments

I attempted to implement C4XGate but got some issues with the implementation of the FixedIntializer traits for arrays of more than 16 elements. There are workarounds, but given its low priority the implementation has been left for a follow-up.

@ElePT ElePT mentioned this pull request Jun 26, 2024
@ElePT ElePT added Rust This PR or issue is related to Rust code in the repository performance Changelog: None Do not include in changelog labels Jun 26, 2024
@ElePT ElePT added this to the 1.2.0 milestone Jun 26, 2024
@coveralls
Copy link

coveralls commented Jun 26, 2024

Pull Request Test Coverage Report for Build 9676345425

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 318 of 353 (90.08%) changed or added relevant lines in 8 files are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.737%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 4 8 50.0%
crates/circuit/src/operations.rs 260 291 89.35%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
crates/qasm2/src/lex.rs 4 92.37%
Totals Coverage Status
Change from base Build 9661630219: 0.02%
Covered Lines: 64048
Relevant Lines: 71373

💛 - Coveralls

@ElePT ElePT added the mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library label Jun 27, 2024
@coveralls
Copy link

coveralls commented Jul 1, 2024

Pull Request Test Coverage Report for Build 9745926823

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 380 of 416 (91.35%) changed or added relevant lines in 9 files are covered.
  • 161 unchanged lines in 6 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.827%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_instruction.rs 8 10 80.0%
qiskit/circuit/quantumcircuit.py 4 8 50.0%
crates/circuit/src/operations.rs 262 292 89.73%
Files with Coverage Reduction New Missed Lines %
qiskit/dagcircuit/dagcircuit.py 4 92.08%
crates/qasm2/src/lex.rs 6 91.6%
crates/qasm2/src/parse.rs 12 97.15%
crates/accelerate/src/euler_one_qubit_decomposer.rs 28 92.06%
crates/circuit/src/circuit_data.rs 31 90.85%
crates/accelerate/src/two_qubit_decompose.rs 80 89.5%
Totals Coverage Status
Change from base Build 9745133547: 0.02%
Covered Lines: 64681
Relevant Lines: 72006

💛 - Coveralls

@ElePT ElePT marked this pull request as ready for review July 2, 2024 08:18
@ElePT ElePT requested review from nonhermitian and a team as code owners July 2, 2024 08:18
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @enavarro51
  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia
  • @kevinhartman
  • @mtreinish

@coveralls
Copy link

coveralls commented Jul 2, 2024

Pull Request Test Coverage Report for Build 9762974381

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 478 of 512 (93.36%) changed or added relevant lines in 9 files are covered.
  • 22 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.004%) to 89.808%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_instruction.rs 8 10 80.0%
crates/circuit/src/operations.rs 350 382 91.62%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/quantumcircuit.py 4 94.44%
crates/qasm2/src/lex.rs 6 92.11%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 9757893531: 0.004%
Covered Lines: 64868
Relevant Lines: 72230

💛 - Coveralls

crates/circuit/src/circuit_instruction.rs Outdated Show resolved Hide resolved
@@ -305,13 +303,13 @@ static STANDARD_GATE_NAME: [&str; STANDARD_GATE_SIZE] = [
"cu", // 39
"cu1", // 40
"cu3", // 41
"c3x", // 42
"c3x", // 42 ("mcx")
Copy link
Member

Choose a reason for hiding this comment

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

I feel like this mismatch will cause a problem eventually. Like it's not an issue right now because we never use these gates out of the box anywhere. But we use these names as the canonical identifier in a lot of places (which is why the overloaded mcx name is a real problem). For example, when we port the qasm exporters to rust they'll just call op.name() internally and then this will return c3x instead of mcx which will cause issues.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. But it seems like the solution would be not using the overloaded mcx name in Python, wouldn't it?

Copy link
Member

Choose a reason for hiding this comment

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

It would, and ideally that's how I would expect us to fix this. But when we discussed potentially doing that as part of #12628 I thought the concern was around backwards compatibility and the consensus was we shouldn't change the name until 2.0?

Copy link
Contributor Author

@ElePT ElePT Jul 2, 2024

Choose a reason for hiding this comment

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

That's what I vaguely remembered. If we want to rename the gates for 2.0, should we then introduce deprecation warnings in 1.2? I am not sure what the protocol is with name changes, we can't add the new path in advance, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

The deprecation path for something like this isn't clear as it's a straight change in behavior. There isn't a good way to signal this to users unless we emit a FutureWarning every time a C3XGate is created. This might be the case we just document this in the 2.0 release notes and say it wasn't easy to raise a deprecation warning for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I didn't see a way around the recent failures other than renaming "c3x" to "mcx" in rust space (f05c9a1). I don't love it, but I guess we can then change both names at the same time?

crates/circuit/src/operations.rs Outdated Show resolved Hide resolved
@coveralls
Copy link

coveralls commented Jul 2, 2024

Pull Request Test Coverage Report for Build 9765116170

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 485 of 520 (93.27%) changed or added relevant lines in 9 files are covered.
  • 12 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.819%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_instruction.rs 14 17 82.35%
crates/circuit/src/operations.rs 350 382 91.62%
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/synthesis/permutation/utils.rs 1 98.96%
qiskit/circuit/quantumcircuit.py 4 94.44%
crates/qasm2/src/lex.rs 7 91.86%
Totals Coverage Status
Change from base Build 9757893531: 0.02%
Covered Lines: 64913
Relevant Lines: 72271

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jul 3, 2024

Pull Request Test Coverage Report for Build 9773297349

Details

  • 485 of 520 (93.27%) changed or added relevant lines in 9 files are covered.
  • 7 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.02%) to 89.887%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_instruction.rs 14 17 82.35%
crates/circuit/src/operations.rs 350 382 91.62%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 93.13%
qiskit/circuit/quantumcircuit.py 4 94.44%
Totals Coverage Status
Change from base Build 9772085193: 0.02%
Covered Lines: 65179
Relevant Lines: 72512

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

This is looking good, thanks for doing all of this. I just have a couple of small inline comments.

crates/circuit/src/circuit_instruction.rs Outdated Show resolved Hide resolved
crates/circuit/src/operations.rs Outdated Show resolved Hide resolved
crates/circuit/src/operations.rs Show resolved Hide resolved
test/python/circuit/test_rust_equivalence.py Outdated Show resolved Hide resolved
@ElePT ElePT requested a review from mtreinish July 3, 2024 15:57
@coveralls
Copy link

coveralls commented Jul 3, 2024

Pull Request Test Coverage Report for Build 9780967332

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 531 of 552 (96.2%) changed or added relevant lines in 9 files are covered.
  • 213 unchanged lines in 9 files lost coverage.
  • Overall coverage decreased (-0.02%) to 89.853%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_instruction.rs 15 17 88.24%
crates/circuit/src/operations.rs 395 414 95.41%
Files with Coverage Reduction New Missed Lines %
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/circuit/quantumcircuit.py 4 94.57%
crates/qasm2/src/lex.rs 5 92.37%
qiskit/circuit/commutation_checker.py 5 96.97%
qiskit/circuit/library/data_preparation/state_preparation.py 6 95.27%
crates/qasm2/src/parse.rs 6 97.61%
crates/circuit/src/circuit_instruction.rs 23 94.8%
crates/circuit/src/dag_node.rs 26 85.44%
crates/circuit/src/operations.rs 137 83.94%
Totals Coverage Status
Change from base Build 9772085193: -0.02%
Covered Lines: 65508
Relevant Lines: 72906

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jul 3, 2024

Pull Request Test Coverage Report for Build 9783170656

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 530 of 552 (96.01%) changed or added relevant lines in 9 files are covered.
  • 236 unchanged lines in 11 files lost coverage.
  • Overall coverage decreased (-0.04%) to 89.83%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_instruction.rs 14 17 82.35%
crates/circuit/src/operations.rs 395 414 95.41%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/expr.rs 1 94.02%
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
qiskit/circuit/quantumcircuit.py 4 94.57%
qiskit/circuit/commutation_checker.py 5 96.97%
qiskit/circuit/library/data_preparation/state_preparation.py 6 95.27%
crates/qasm2/src/lex.rs 7 92.11%
crates/circuit/src/circuit_data.rs 9 90.73%
crates/qasm2/src/parse.rs 18 96.69%
crates/circuit/src/circuit_instruction.rs 22 94.79%
crates/circuit/src/dag_node.rs 26 85.44%
Totals Coverage Status
Change from base Build 9772085193: -0.04%
Covered Lines: 65485
Relevant Lines: 72899

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

I think this looks good to go now. I just a had a few very small comments inline. The only other question I have is about c4x if we're not going to implement it in rust should we remove it from the list?

crates/circuit/src/circuit_instruction.rs Show resolved Hide resolved
crates/circuit/src/operations.rs Show resolved Hide resolved
test/python/circuit/test_rust_equivalence.py Outdated Show resolved Hide resolved

SKIP_LIST = {"rx", "ry", "ecr"}
CUSTOM_MAPPING = {"x", "rz"}
CUSTOM_NAME_MAPPING = {"mcx": C3XGate()}
MATRIX_SKIP_LIST = {"c3sx"}
Copy link
Member

Choose a reason for hiding this comment

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

I thought I saw the rust definition of this matrix using the macro in the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the test is being skipped because of the python definition, not the rust one.
qiskit.circuit.exceptions.CircuitError: "to_matrix not defined for this <class '_SingletonC3SXGate'>"

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, that seems like a bug in the python side then. But lets fix that in a separate PR

Copy link
Contributor Author

@ElePT ElePT Jul 8, 2024

Choose a reason for hiding this comment

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

I will try to find a fix tomorrow in a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@coveralls
Copy link

Pull Request Test Coverage Report for Build 9842649589

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 530 of 552 (96.01%) changed or added relevant lines in 9 files are covered.
  • 248 unchanged lines in 13 files lost coverage.
  • Overall coverage decreased (-0.01%) to 89.859%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/circuit_instruction.rs 14 17 82.35%
crates/circuit/src/operations.rs 395 414 95.41%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/library/standard_gates/x.py 1 98.03%
qiskit/quantum_info/operators/symplectic/clifford.py 1 91.87%
crates/accelerate/src/synthesis/linear/mod.rs 2 93.55%
qiskit/circuit/quantumcircuit.py 4 94.57%
qiskit/circuit/commutation_checker.py 5 96.97%
qiskit/circuit/library/data_preparation/state_preparation.py 6 95.27%
crates/qasm2/src/lex.rs 6 92.62%
crates/circuit/src/circuit_data.rs 9 90.73%
crates/qasm2/src/parse.rs 12 97.15%
crates/circuit/src/slice.rs 17 83.85%
Totals Coverage Status
Change from base Build 9772085193: -0.01%
Covered Lines: 65589
Relevant Lines: 72991

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

This LGTM now, thanks for doing this. The only open question is around whether we should remove c4x from the list. But we can decide that in a follow up, or add a comment. It's more effort to remove it as part of this since it already had a placeholder.

@mtreinish mtreinish enabled auto-merge July 8, 2024 16:07
@mtreinish mtreinish added this pull request to the merge queue Jul 8, 2024
Merged via the queue into Qiskit:main with commit 4867e8a Jul 8, 2024
15 checks passed
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
* Add C3X (MCX), extend rust tests to multi-controlled gates.

* Add macro to generate multi-controlled gates. Add CU, CU1, CU3, C3SX, C4X, CCZ.

* Kill C4XGate

* Finish adding gates, add circuit construction methods when possible.

* Add import paths, fix drawer test.

* Establish CGates with non-default control states as non-standard in circuit_instruction.rs. Add unit test.

* Fix merge conflicts

* Apply macro on missing gates

* Add RCCX gate and RC3X (RCCCX) gate.

* Make equivalence tests more explicit

* Fix lint

* Modify circuit methods for consistency

* Fix default ctrl state for 3q+ gates, add test for CCZ

* Apply comments from Matt's code review

* Fix ctrl_state logic

* Rename c3x to mcx?

* Brackets didn't match explanation

* Make sure controlled test doesn't use custom ControlledGate instances.

* Rename c4x to mcx in Rust space.

* Return PyResult rather than panic on error

* Add suggestion from Matt's code review

Co-authored-by: Matthew Treinish <[email protected]>

---------

Co-authored-by: John Lapeyre <[email protected]>
Co-authored-by: Matthew Treinish <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library performance Rust This PR or issue is related to Rust code in the repository
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants