Skip to content

Conversation

mtreinish
Copy link
Member

Summary

This commit adds the rust representation of the U1Gate, U2Gate, and U3Gate to the StandardGates enum in rust.

Details and comments

Part of #12566

@mtreinish mtreinish added performance Changelog: None Do not include in changelog Rust This PR or issue is related to Rust code in the repository labels Jun 13, 2024
@mtreinish mtreinish added this to the 1.2.0 milestone Jun 13, 2024
@mtreinish mtreinish requested a review from a team as a code owner June 13, 2024 13:11
@qiskit-bot
Copy link
Collaborator

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

  • @Eric-Arellano
  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia
  • @kevinhartman
  • @mtreinish

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

Overall LGTM, just two questions from my side 🙂

@@ -374,6 +382,23 @@ impl Operation for StandardGate {
}
_ => None,
},
Self::U1Gate => match params[0] {
Param::Float(val) => Some(aview2(&gate_matrix::u1_gate(val)).to_owned()),
Copy link
Contributor

@ElePT ElePT Jun 18, 2024

Choose a reason for hiding this comment

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

I've noticed that &gate_matrix::u1_gate(val) is not dereferencing val, while the other gate calls do: &gate_matrix::u2_gate(*phi, *lam), u3_gate(*theta, *phi, *lam). Was this intentional? (aha, it's because it's just a float, never mind).

Copy link
Member Author

Choose a reason for hiding this comment

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

The difference here is that the match is on params[0] instead of params. The val is already an f64 and you can't dereference that, while for the other gates the angles have &f64.

I haven't dug into the test failures yet, but I suspect it's more likely I screwed up something in the translation. The qasm3 failure at least is because there are no longer fixed object ids for the gates and I need to update the test to account for that (i.e. use a regex).

Copy link
Member Author

Choose a reason for hiding this comment

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

If this is a blocker for #12598 feel free to just rebase it on main and move ahead without this. I can reindex the gates in this PR without too much trouble.

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed the test failures in: 65ff009 the optimize 1q gates decomposition failures were because the tests were doing:

gate.definition.global_phase = val

and that doesn't do anything to the gate object for a standard gate in rust. TBH, I have no idea what this test is trying to do but to fix it I just used a custom gate class instead of U1Gate.

Copy link
Contributor

Choose a reason for hiding this comment

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

So gate.definition.global_phase = val is another of the "hidden forbidden moves", like dag_node.op.params[0]. Do you think we should document it in the reno too? At a first glance people would probably not expect it to fail.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure it needs one, the definition was never really supposed to be mutable like this as it's an inherent attribute of a gate, changing it doesn't make much sense (for most things it doesn't get used either). This already wouldn't work with singletons as changing the global phase would affect every instance of the gate. I viewed this as test bug, because I have no idea what that test was trying to accomplish. But I can add a release note out of an abundance of caution if you think it's warranted here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm I think it would be nice to have it documented somewhere, but I am ok just keeping an eye on it, I don't think people refer to the release notes anywhere and I see how documenting every edge case can be a problem.

mtreinish and others added 3 commits June 21, 2024 13:38
This commit adds the rust representation of the U1Gate, U2Gate, and
U3Gate to the `StandardGates` enum in rust.

Part of Qiskit#12566
Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
@coveralls
Copy link

coveralls commented Jun 21, 2024

Pull Request Test Coverage Report for Build 9618403783

Details

  • 82 of 85 (96.47%) changed or added relevant lines in 5 files are covered.
  • 11 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.008%) to 89.786%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/operations.rs 50 53 94.34%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 3 92.88%
crates/qasm2/src/parse.rs 6 97.61%
Totals Coverage Status
Change from base Build 9616513556: 0.008%
Covered Lines: 63739
Relevant Lines: 70990

💛 - Coveralls

@coveralls
Copy link

coveralls commented Jun 21, 2024

Pull Request Test Coverage Report for Build 9618804762

Details

  • 82 of 85 (96.47%) changed or added relevant lines in 5 files are covered.
  • 7 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.01%) to 89.792%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/operations.rs 50 53 94.34%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.2%
crates/qasm2/src/lex.rs 5 92.37%
Totals Coverage Status
Change from base Build 9616513556: 0.01%
Covered Lines: 63743
Relevant Lines: 70990

💛 - Coveralls

@mtreinish mtreinish requested a review from ElePT June 21, 2024 21:56
Copy link
Contributor

@ElePT ElePT left a comment

Choose a reason for hiding this comment

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

The implementation LGTM, I just had a couple of questions about the cases that triggered test failures.

@@ -102,7 +113,7 @@ def test_matrix(self):
continue

with self.subTest(name=name):
params = [pi] * standard_gate._num_params()
params = [0.1] * standard_gate._num_params()
Copy link
Contributor

Choose a reason for hiding this comment

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

Was this change because math.pi was triggering some of the test failures? I feel like it's commonly used for assigning parameter values.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I thought I changed it back. While debugging I did this because one of the test failures was using 0.1 as the angle value and I wanted to rule that out as the cause of the failure (checking the matrix was also correct with 0.1). I can change it back to pi, it realistically doesn't matter too much though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd even expect 0.1 to be a better value for testing since for pi the matrices take on some special forms and 0.1 is more "irregular" in that sense 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good, I just wanted to make sure we were not ignoring pi.

@@ -374,6 +382,23 @@ impl Operation for StandardGate {
}
_ => None,
},
Self::U1Gate => match params[0] {
Param::Float(val) => Some(aview2(&gate_matrix::u1_gate(val)).to_owned()),
Copy link
Contributor

Choose a reason for hiding this comment

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

So gate.definition.global_phase = val is another of the "hidden forbidden moves", like dag_node.op.params[0]. Do you think we should document it in the reno too? At a first glance people would probably not expect it to fail.

@ElePT ElePT added this pull request to the merge queue Jun 24, 2024
Merged via the queue into Qiskit:main with commit bf8f398 Jun 24, 2024
@mtreinish mtreinish deleted the add-u123-to-rs branch June 24, 2024 18:26
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
* Add rust representation for the u1, u2, and u3 gates

This commit adds the rust representation of the U1Gate, U2Gate, and
U3Gate to the `StandardGates` enum in rust.

Part of Qiskit#12566

* Update crates/circuit/src/imports.rs

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>

* Fix test failures

* Fix pylint pedantry

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
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 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