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

Fix qpy support for Cliffords #11495

Merged
merged 8 commits into from
Jan 17, 2024
Merged

Conversation

alexanderivrii
Copy link
Contributor

Summary

This PR adds QPY support for Cliffords, partially fixing #11088.

@alexanderivrii alexanderivrii requested a review from a team as a code owner January 5, 2024 09:54
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

  • @Qiskit/terra-core
  • @mtreinish
  • @nkanazawa1989

@coveralls
Copy link

coveralls commented Jan 5, 2024

Pull Request Test Coverage Report for Build 7553589438

Warning: This coverage report may be inaccurate.

We've detected an issue with your CI configuration that might affect the accuracy of this pull request's coverage report.
To ensure accuracy in future PRs, please see these guidelines.
A quick fix for this PR: rebase it; your next report should be accurate.

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.005%) to 89.334%

Totals Coverage Status
Change from base Build 7552937080: 0.005%
Covered Lines: 59394
Relevant Lines: 66485

💛 - Coveralls

Cryoris
Cryoris previously approved these changes Jan 8, 2024
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.

Maybe @mtreinish would like a final look but LGTM 🙂

Cryoris
Cryoris previously approved these changes Jan 11, 2024
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.

Overall this LGTM thanks for the fix. I left one question inline about whether there is a potential simplification to the read logic.

The only other thing we maybe want to add is a backwards compatibility test to https://github.com/Qiskit/qiskit/blob/main/test/qpy_compat/test_qpy.py ? I'm not sure it's needed but if you think there's value in validating that we can load a Clifford with newer qiskit from an historical release it would be good to add there (moving forward we'd obviously have to limit the version strings, see: https://github.com/Qiskit/qiskit/blob/main/test/qpy_compat/test_qpy.py#L750).

@@ -290,13 +290,17 @@ def _read_instruction(
gate_class = getattr(quantum_initializer, gate_name)
elif hasattr(controlflow, gate_name):
gate_class = getattr(controlflow, gate_name)
elif gate_name == "Clifford":
pass
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be?

Suggested change
pass
gate_class = Clifford

Mostly just for consistency. But if we did this then we would be able to drop the special handling in L302 as it would just call gate_class(*params). Or is len(Clifford.params) > 1 and doing this wouldn't work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in bc1f5e3.

@mtreinish mtreinish added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: Bugfix Include in the "Fixed" section of the changelog mod: qpy Related to QPY serialization labels Jan 11, 2024
@mtreinish mtreinish added this to the 0.45.2 milestone Jan 11, 2024
@mtreinish
Copy link
Member

I've tagged this as stable backport potential because this is something we can fix for 0.45.x, especially as there is no version change to the QPY format.

@alexanderivrii
Copy link
Contributor Author

Since you have mentioned this, I think that adding a backwards compatibility Cliffords test might be a good idea, this is done 5f42977. The only thing I am not 100% sure is whether the line limiting the version strings should read version_parts >= (0, 25, 0):.

@alexanderivrii
Copy link
Contributor Author

Update: I don't understand why the backward compatibility test fails: the claimed error

  File "/home/vsts/work/1/s/test/qpy_compat/1.0.0b1/lib/python3.8/site-packages/qiskit/qpy/binary_io/circuits.py", line 581, in _write_instruction
    gate_class_name = instruction.operation.base_class.__name__
AttributeError: 'Clifford' object has no attribute 'base_class'

is exactly what was fixed in this PR. Or is this because the milestone for this PR is set to 0.45.2 but it should be 0.46.0 or something like this?

@Cryoris
Copy link
Contributor

Cryoris commented Jan 16, 2024

It looks like the compatibility tests fail because you've added a Clifford based test which is run with a Qiskit version prior to your PR. If you check the line number where the error is raised (581) that refers to the line of the code that does not exist in your PR 🙂 I'm assuming we have to enable the new Clifford test in qpy_compat/test_qpy.py only for code versions with your changes.

@alexanderivrii
Copy link
Contributor Author

Thanks @Cryoris, which version then we should enable? I thought it's 0.45.2 for which the PR is tagged, but I guess it should be higher than this?

@mtreinish
Copy link
Member

0.45.2 is correct assuming we're going to backport this (which I think we should). The actual issue causing the failure is because of the 1.0.0b1 release is being treated as > 0.45.2 (which it is) but doesn't contain this PR. We probably need to exclude the 1.0.0b1 release from the compat tests because it's not real and doesn't have a stable api.

@mtreinish
Copy link
Member

I pushed up #11572 to address this.

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, thanks for the updates.

@@ -640,6 +640,21 @@ def generate_layout_circuits():
return [qc]


def generate_clifford_circuits():
"""Test qpy circuits with Clifford operations."""
from qiskit.quantum_info import Clifford
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure the runtime import is strictly needed as this is only necessary if the import doesn't exist on all the historical versions that runs this file. But in the case of Clifford that should exist in 0.18.0 (which is the oldest version tested with this, which added qpy). But it doesn't make a difference in practice.

@mtreinish mtreinish added this pull request to the merge queue Jan 17, 2024
Merged via the queue into Qiskit:main with commit 786c0e9 Jan 17, 2024
13 checks passed
mergify bot pushed a commit that referenced this pull request Jan 17, 2024
* clifford qpy support + test

* release notes

* cleanup: checking if instruction is of type Instruction

* review comment

* review comments: consistency + remove specialized handling

* adding test to test_qpy.py

* changing version string to 0.45.2

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
(cherry picked from commit 786c0e9)
github-merge-queue bot pushed a commit that referenced this pull request Jan 17, 2024
* clifford qpy support + test

* release notes

* cleanup: checking if instruction is of type Instruction

* review comment

* review comments: consistency + remove specialized handling

* adding test to test_qpy.py

* changing version string to 0.45.2

---------

Co-authored-by: Elena Peña Tapia <57907331+ElePT@users.noreply.github.com>
(cherry picked from commit 786c0e9)

Co-authored-by: Alexander Ivrii <alexi@il.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Bugfix Include in the "Fixed" section of the changelog mod: qpy Related to QPY serialization stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants