Skip to content

Implement a builder-like constructor for Target#14308

Open
raynelfss wants to merge 10 commits into
Qiskit:mainfrom
raynelfss:target-expose-new
Open

Implement a builder-like constructor for Target#14308
raynelfss wants to merge 10 commits into
Qiskit:mainfrom
raynelfss:target-expose-new

Conversation

@raynelfss

@raynelfss raynelfss commented May 6, 2025

Copy link
Copy Markdown
Contributor

Summary

In an oversight during #14228, we forgot to expose the Target::new() method to the rest of the crate. The following commits expose a Rust native Target::new() as well as some other methods which enable us to construct the Target using a builder-like architecture.

Details

The following commits add a rust native constructor for the Target that uses a builder-like architecture to add all of the specified attributes in a more dynamic manner.

// To create a target with a specified description and number of qubits:
let sydney_target: Target = Target::new()
    .with_description("Sydney-Target")
    .with_num_qubits(4)
    .unwrap();

// To create a target with a specific dt value:
let seoul_target: Target = Target::new()
    .with_dt(10e-9)
    .unwrap();

The methods include the following:

  • new - Rust native constructor, must be called before using any builder methods. Equivalent to Default::default().
  • with_description - To create a target with a specific description.
  • with_num_qubits - (Fallible) Assigns an explicit number of qubits to the Target, may fail if the specified qubit_properties list length differs from the value of this number.
  • with_dt - Assigns the dt value.
  • with_granularity - Assigns a granularity value in units of dt.
  • with_min_length - Assigns a minimal pulse length in units of dt.
  • with_pulse_alignment - Assigns a gate instruction starting time.
  • with_acquire_alignment - Assigns a measure instruction starting time.
  • with_qubit_properties - (Fallible) Specifies the properties of each of the backend's qubits, if associated. May fail if the specified qubit_properties list length differs from the value of num_qubits.
  • with_concurrent_measurements - Specifies which qubits need to be measured together.

@raynelfss raynelfss requested a review from a team as a code owner May 6, 2025 15:36
@raynelfss raynelfss added the Rust This PR or issue is related to Rust code in the repository label May 6, 2025
@qiskit-bot

Copy link
Copy Markdown
Collaborator

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

  • @Qiskit/terra-core

@jakelishman jakelishman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rust doesn't have keyword arguments, so a new method that's 8 Option<T>s long perhaps isn't the cleanest user interface. Perhaps a builder-like interface would be cleaner?

let mut target = Target::new()
    .with_description("target")
    .with_num_qubits(4)
    .with_dt(10e-9);

or such?

@coveralls

coveralls commented May 6, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 21005445683

Details

  • 171 of 173 (98.84%) changed or added relevant lines in 4 files are covered.
  • 19 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+0.003%) to 88.32%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/transpiler/src/target/mod.rs 165 167 98.8%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 3 92.54%
crates/circuit/src/parameter/symbol_expr.rs 4 72.98%
crates/qasm2/src/parse.rs 12 96.62%
Totals Coverage Status
Change from base Build 21005190367: 0.003%
Covered Lines: 97056
Relevant Lines: 109891

💛 - Coveralls

@raynelfss raynelfss force-pushed the target-expose-new branch from 6613a3f to 7a9d177 Compare May 6, 2025 19:46
@raynelfss raynelfss changed the title Expose Target::new() to the rest of the crates Implement a builder-like constructor for Target May 7, 2025
@raynelfss raynelfss requested a review from jakelishman May 7, 2025 12:35
@eliarbel eliarbel added this to the 2.1.0 milestone May 8, 2025
@raynelfss

Copy link
Copy Markdown
Contributor Author

Out of curiosity, would it be easier to just have the constructor setter methods for num_qubits and qubit_properties (or have them together as one method), and allow the user to edit the rest of the values (dt, minimal_length, granularity pulse_alignment, acquire_alignment and concurrent_measurements)? Since all of these attributes are public and the only ones that are fallible are num_qubits and qubit_properties in respect to each other values, it would make sense to allow the users to do something like:

let mut target = Target::new().try_with_num_qubits(5)?.try_with_qubit_properties(vec![])?;
target.dt = Some(10e-9);
target.min_length = 2;

Comment thread crates/transpiler/src/target/mod.rs Outdated
Comment thread crates/transpiler/src/target/mod.rs Outdated
Comment thread crates/transpiler/src/target/mod.rs Outdated
Comment thread crates/transpiler/src/target/mod.rs Outdated
Comment thread crates/transpiler/src/target/mod.rs Outdated
Comment thread crates/transpiler/src/target/mod.rs Outdated
/// let mut target = Target::new().with_qubit_properties(vec![]);
/// assert_eq!(target.num_qubits, Some(0));
/// ```
pub fn with_qubit_properties(self, qubit_properties: Vec<PyObject>) -> Self {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this actually a thing we should have in the rust builder api? It doesn't seem like it's something we'll ever define in Rust while it's a PyObject.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not until we have qubit properties in Rust. We may want to have this ready as a placeholder though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we have an issue tracking the move of qubit properties to rust?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes #14306

Comment thread crates/transpiler/src/target/mod.rs Outdated
Comment thread crates/transpiler/src/target/mod.rs
Comment thread crates/transpiler/src/target/mod.rs Outdated
Comment thread crates/transpiler/src/target/mod.rs Outdated
///
/// Args:
/// operation (str): The operation name to get qargs for
/// Returns:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this correct? In sphinx-napoleon/google style which we use for qiskit it should be Returns:. See:https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Will change it back to Returns:

@mtreinish mtreinish modified the milestones: 2.1.0, 2.2.0 Jun 4, 2025
@mtreinish

Copy link
Copy Markdown
Member

Given the timeframe and that this is a good to have internal usability thing I deferred this 2.2 to minimize the risk to 2.1.

@raynelfss raynelfss added the Changelog: None Do not include in the GitHub Release changelog. label Jul 7, 2025
@raynelfss raynelfss modified the milestones: 2.2.0, 2.3.0 Sep 2, 2025
@github-project-automation github-project-automation Bot moved this to Ready in Qiskit 2.3 Oct 7, 2025
@raynelfss raynelfss requested a review from mtreinish November 18, 2025 19:08
@Cryoris

Cryoris commented Dec 9, 2025

Copy link
Copy Markdown
Contributor

Basically the same reason to defer to 2.4 given that we're 2 days off RC1:

Given the timeframe and that this is a good to have internal usability thing I deferred this 2.2 to minimize the risk to 2.1.

@Cryoris Cryoris modified the milestones: 2.3.0, 2.4.0 Dec 9, 2025
@github-project-automation github-project-automation Bot moved this to Ready in Qiskit 2.4 Dec 9, 2025
@Cryoris Cryoris removed this from Qiskit 2.3 Dec 9, 2025
@raynelfss raynelfss requested a review from Cryoris February 19, 2026 19:24
@alexanderivrii alexanderivrii modified the milestones: 2.4.0, 2.5.0 Mar 19, 2026
raynelfss added a commit to raynelfss/qiskit that referenced this pull request Apr 16, 2026
Add: Builder-like construction methods.
- Add detailed docstrings with warnings about where not to use them.

Add: Use builder-like construction in `py_new`

Fix: Separate result-handlling and panic versions of `num_qubit` and `qubit_properties` initializers.

Test: Add rust unit tests.

FIx: Address review comments

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

Chore: Use `Returns:` instead of `Returns` in docstrings

Fix: Adapt to c changes
raynelfss added a commit to raynelfss/qiskit that referenced this pull request Apr 27, 2026
Add: Builder-like construction methods.
- Add detailed docstrings with warnings about where not to use them.

Add: Use builder-like construction in `py_new`

Fix: Separate result-handlling and panic versions of `num_qubit` and `qubit_properties` initializers.

Test: Add rust unit tests.

FIx: Address review comments

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>

Chore: Use `Returns:` instead of `Returns` in docstrings

Fix: Adapt to c changes
raynelfss and others added 10 commits June 10, 2026 11:22
- Add detailed docstrings with warnings about where not to use them.
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
The following commit removed `with_num_qubits()`, `with_qubit_properties()`. in favor of the fallible methods.
Methods have been updated to use these additions.
@raynelfss raynelfss force-pushed the target-expose-new branch from cac6199 to 98f6e21 Compare June 10, 2026 15:42
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 the GitHub Release changelog. Rust This PR or issue is related to Rust code in the repository

Projects

Status: Ready

Development

Successfully merging this pull request may close these issues.

8 participants