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

Fully port GatesInBasis to Rust. #13034

Merged
merged 4 commits into from
Sep 26, 2024
Merged

Conversation

kevinhartman
Copy link
Contributor

@kevinhartman kevinhartman commented Aug 25, 2024

Summary

Ports GatesInBasis to Rust. The implementation avoids the prior conversion to DAGCircuit for each control flow block, which should help with performance.

Details and comments

Additional changes:

  • Adds a PyInstruction::blocks() for passes which need to get the blocks of a control flow operation as native CircuitData.
  • Adds NodeType::unwrap_operation() for callers who know that a DAG node is an operation.
  • Makes Target interface public for use in other crates (needed to accept a &Target in a pyfunction).
  • Fixes some mutability / visibility concerns in DAGCircuit, naming making dag, qubits, and clbits private again.

Includes some changes from #13056, #13013 and #13006 which ought to merge first.

Resolves #12275

@kevinhartman kevinhartman requested a review from a team as a code owner August 25, 2024 18:12
@qiskit-bot
Copy link
Collaborator

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

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

@coveralls
Copy link

coveralls commented Aug 25, 2024

Pull Request Test Coverage Report for Build 11058624835

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 89 of 90 (98.89%) changed or added relevant lines in 5 files are covered.
  • 173 unchanged lines in 9 files lost coverage.
  • Overall coverage increased (+0.05%) to 88.847%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/circuit/src/dag_circuit.rs 4 5 80.0%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/layout/disjoint_utils.py 1 96.83%
qiskit/circuit/instruction.py 4 95.31%
crates/qasm2/src/lex.rs 5 92.73%
crates/qasm2/src/parse.rs 6 97.61%
qiskit/transpiler/passes/basis/basis_translator.py 8 97.45%
qiskit/transpiler/preset_passmanagers/builtin_plugins.py 11 96.46%
crates/circuit/src/dag_circuit.rs 17 87.84%
qiskit/circuit/equivalence.py 24 27.27%
qiskit/circuit/quantumcircuit.py 97 93.33%
Totals Coverage Status
Change from base Build 10947873036: 0.05%
Covered Lines: 74015
Relevant Lines: 83306

💛 - Coveralls

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.

Thanks @kevinhartman for this PR. I know it's currently blocked by #13056 but I wanted to take a look now to get more familiar with the latest contributions to our Rust crates. From my (limited) point of view, the changes looks good, I really like the additions you made beyond the GatesInBasis pass. I did get confused by some changes but then realized that the branch is a bit out of date so I can take a second look once it's updated.

crates/accelerate/src/gates_in_basis.rs Show resolved Hide resolved
crates/accelerate/src/gates_in_basis.rs Outdated Show resolved Hide resolved
Comment on lines 81 to 91
impl NodeType {
pub fn unwrap_operation(&self) -> &PackedInstruction {
match self {
NodeType::Operation(instr) => instr,
_ => panic!("Node is not an operation!"),
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I may just go ahead and borrow this in my UnitarySynthesis WIP PR now because it's so much nicer than having to unwrap nodes manually (this will be merged earlier anyways).

Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

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

This looks about ready to merge. I just have a couple comments about Target's modified visibility and some extra nitpicks.

crates/accelerate/src/gates_in_basis.rs Outdated Show resolved Hide resolved
Comment on lines +80 to +88
// In the outer DAG, virtual and physical bits are the same thing.
let wire_map: HashMap<Qubit, PhysicalQubit> =
HashMap::from_iter((0..dag.num_qubits()).map(|i| {
(
Qubit(i.try_into().unwrap()),
PhysicalQubit::new(i.try_into().unwrap()),
)
}));

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this mapping here mainly to reduce tha cost of re-building Qubits or PhysicalQubit instances from each other? Not that it needs any correction I just wanted to understand if this was the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The purpose is actually to map qubits within control flow blocks from the indexing scheme used within the current block back to the indexing of the root circuit.

For example, if the root circuit has 5 qubits and has an if-else operation that uses qubits 3 and 4, then the inner circuits corresponding to the if and else blocks of that operation will have 2 qubits, indexed 0 and 1. To get from 0 and 1 each inner block's indexing scheme to 3 and 4 in the root circuit's indexing scheme, we use this map.

crates/circuit/src/dag_circuit.rs Show resolved Hide resolved
crates/accelerate/src/gates_in_basis.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@raynelfss raynelfss left a comment

Choose a reason for hiding this comment

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

LGTM!

@raynelfss raynelfss added this pull request to the merge queue Sep 26, 2024
Merged via the queue into Qiskit:main with commit 8ccbc8d Sep 26, 2024
15 checks passed
ElePT pushed a commit to ElePT/qiskit that referenced this pull request Oct 9, 2024
* Fully port gates_in_basis to Rust.

* Revert visibility to no longer public.

* Add docstring for unwrap_operation.

* Remove unused py token.
@ElePT ElePT added the Changelog: None Do not include in changelog label Nov 6, 2024
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Port GatesInBasis to Rust
6 participants