Skip to content

Add QuantumCircuit.ensure_physical #14779

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jakelishman
Copy link
Member

This is similar but stronger to DAGCircuit::make_physical. Since QuantumCircuit also has layout information available, this method safely handles this, and the case that the circuit has already been made physical.

The new documentation in the circuit module and the QuantumCircuit class is intended to help, since this is a common point of confusion among both users and developers; the documentation isn't supposed to be a strong personal endorsement of the current situation, just an explicit statement of what the current system is.

This commit was motivated by ad-hoc attempts to do this, such as in QuantumCircuit._from_circuit_data(add_regs=True) (which can be updated to use this in a follow-up).

Summary

Details and comments

The reason that the QuantumCircuit method is called ensure_physical and the DAGCircuit one is called make_physical is because the DAG one doesn't have the same information available to it about what (if anything) the current layout is (in the current Qiskit data flow, the layout, if any, is in the PropertySet while transpilation is ongoing), so it can't really do a proper verification; it's always an active modification. The QuantumCircuit variant can tell if a circuit is already laid out.

This is similar but stronger to `DAGCircuit::make_physical`.  Since
`QuantumCircuit` also has layout information available, this method
safely handles this, and the case that the circuit has already been made
physical.

The new documentation in the circuit module and the `QuantumCircuit`
class is intended to help, since this is a common point of confusion
among both users and developers; the documentation isn't supposed to be
a strong personal endorsement of the current situation, just an explicit
statement of what the current system _is_.

This commit was motivated by ad-hoc attempts to do this, such as in
`QuantumCircuit._from_circuit_data(add_regs=True)` (which can be updated
to use this in a follow-up).
@jakelishman jakelishman added this to the 2.2.0 milestone Jul 22, 2025
@jakelishman jakelishman requested a review from a team as a code owner July 22, 2025 13:10
@jakelishman jakelishman added documentation Something is not clear or an error documentation Changelog: New Feature Include in the "Added" section of the changelog mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library labels Jul 22, 2025
@qiskit-bot
Copy link
Collaborator

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

  • @Qiskit/terra-core

Copy link
Contributor

@ihincks ihincks left a comment

Choose a reason for hiding this comment

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

👍

/// Args:
/// num_qubits: if given, the total number of physical qubits in the output; it must be at
/// least as large as the number of qubits in the circuit. If not given, the number of
/// qubits is unchanged.
Copy link
Contributor

Choose a reason for hiding this comment

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

You know qiskit style better than me, but I'll mention in case it is an oversight rather than intentional: do you want a Raises: section here?

Copy link
Member Author

Choose a reason for hiding this comment

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

we're pretty lax with "Raises" sections - I don't usually add them when the only exceptions are things like ValueError for things that are fairly obviously incorrect inputs. If you go down that route, basically everything in Python can raise runtime exceptions (TypeError/IndexError/ValueError and all sorts), so I tend to only put them for non-obvious Qiskit-specific things.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 16445290736

Details

  • 55 of 62 (88.71%) changed or added relevant lines in 2 files are covered.
  • 5 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.001%) to 87.781%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 19 20 95.0%
crates/circuit/src/circuit_data.rs 36 42 85.71%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 91.75%
Totals Coverage Status
Change from base Build 16420779601: -0.001%
Covered Lines: 81543
Relevant Lines: 92894

💛 - Coveralls

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 documentation Something is not clear or an error documentation mod: circuit Related to the core of the `QuantumCircuit` class or the circuit library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants