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 Expr support to QuantumCircuit.compose #10375

Merged
merged 1 commit into from
Jul 19, 2023

Conversation

jakelishman
Copy link
Member

Summary

This relatively straightforwardly generalises the mapping of variables that already exists in QuantumCircuit.compose to be able to handle arbitrary Expr nodes as well. We must take care not to accidentally mutate the Expr nodes in the input circuit in the name of efficiency.

Details and comments

Resolve #10227. Depends on #10359. Changelog to come in #10331.

@jakelishman jakelishman added the Changelog: New Feature Include in the "Added" section of the changelog label Jul 3, 2023
@jakelishman jakelishman added this to the 0.25.0 milestone Jul 3, 2023
@jakelishman jakelishman requested a review from a team as a code owner July 3, 2023 17:51
@qiskit-bot
Copy link
Collaborator

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

@coveralls
Copy link

coveralls commented Jul 3, 2023

Pull Request Test Coverage Report for Build 5598447243

  • 52 of 54 (96.3%) changed or added relevant lines in 1 file are covered.
  • 9 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.02%) to 86.07%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 52 54 96.3%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 1 92.15%
qiskit/extensions/quantum_initializer/squ.py 2 80.0%
crates/qasm2/src/parse.rs 6 97.11%
Totals Coverage Status
Change from base Build 5597428209: 0.02%
Covered Lines: 72729
Relevant Lines: 84500

💛 - Coveralls

This relatively straightforwardly generalises the mapping of variables
that already exists in `QuantumCircuit.compose` to be able to handle
arbitrary `Expr` nodes as well.  We must take care not to accidentally
mutate the `Expr` nodes in the input circuit in the name of efficiency.
@jakelishman
Copy link
Member Author

Rebased over main.

@jakelishman jakelishman removed the on hold Can not fix yet label Jul 19, 2023
@kevinhartman kevinhartman self-assigned this Jul 19, 2023
Copy link
Contributor

@kevinhartman kevinhartman left a comment

Choose a reason for hiding this comment

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

LGTM!

Comment on lines +5293 to +5303
def visit_value(self, node, /):
return expr.Value(node.value, node.type)

def visit_unary(self, node, /):
return expr.Unary(node.op, node.operand.accept(self), node.type)

def visit_binary(self, node, /):
return expr.Binary(node.op, node.left.accept(self), node.right.accept(self), node.type)

def visit_cast(self, node, /):
return expr.Cast(node.operand.accept(self), node.type, implicit=node.implicit)
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it wouldn't really come up again, but if it does, it might make sense to have a transformer base class specific for expr.Expr that has default implementations like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean like a subclass of ExprVisitor that (by default) copies the structure by producing the exact same Expr, but visiting each child node, with the intention that a consumer overrides only the behaviour they need?

I could potentially see that being useful, yeah - I'd maybe like to wait to see if we have other use-cases for it first, before adding more API surface.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, exactly 😄. Totally agree, makes sense to wait.

@kevinhartman kevinhartman added this pull request to the merge queue Jul 19, 2023
Merged via the queue into Qiskit:main with commit a7fcf23 Jul 19, 2023
@jakelishman jakelishman deleted the expr/qc-compose branch July 19, 2023 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: New Feature Include in the "Added" section of the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for Expr values to QuantumCircuit.compose
5 participants