Skip to content

Conversation

maximizemaxwell
Copy link
Contributor

@maximizemaxwell maximizemaxwell commented May 9, 2025

Add C API for unitary gate

Part of issue 14238

#14238

What does this PR do?

Add new function of unitary gate in crates/cext/src/circuit.rs.

Who can review?

@mtreinish @eliarbel @Cryoris

@maximizemaxwell maximizemaxwell requested a review from a team as a code owner May 9, 2025 03:01
@qiskit-bot qiskit-bot added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label May 9, 2025
@qiskit-bot
Copy link
Collaborator

Thank you for opening a new pull request.

Before your PR can be merged it will first need to pass continuous integration tests and be reviewed. Sometimes the review process can be slow, so please be patient.

While you're waiting, please feel free to review other open PRs. While only a subset of people are authorized to approve pull requests for merging, everyone is encouraged to review open pull requests. Doing reviews helps reduce the burden on the core team and helps make the project's code better for everyone.

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

  • @Qiskit/terra-core

@CLAassistant
Copy link

CLAassistant commented May 9, 2025

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

coveralls commented May 9, 2025

Pull Request Test Coverage Report for Build 15296975102

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

  • 39 of 56 (69.64%) changed or added relevant lines in 1 file are covered.
  • 677 unchanged lines in 24 files lost coverage.
  • Overall coverage decreased (-0.5%) to 87.822%

Changes Missing Coverage Covered Lines Changed/Added Lines %
crates/cext/src/circuit.rs 39 56 69.64%
Files with Coverage Reduction New Missed Lines %
crates/quantum_info/src/convert_2q_block_matrix.rs 1 92.65%
crates/transpiler/src/passes/optimize_1q_gates_decomposition.rs 1 90.8%
qiskit/circuit/library/generalized_gates/gms.py 2 94.44%
crates/quantum_info/src/versor_u2.rs 3 81.85%
qiskit/circuit/library/generalized_gates/diagonal.py 3 95.16%
crates/qasm2/src/lex.rs 4 91.23%
qiskit/circuit/library/generalized_gates/permutation.py 4 92.73%
qiskit/circuit/library/arithmetic/piecewise_linear_pauli_rotations.py 5 92.47%
qiskit/circuit/library/blueprintcircuit.py 7 94.31%
qiskit/qasm3/init.py 7 85.45%
Totals Coverage Status
Change from base Build 15216054136: -0.5%
Covered Lines: 79864
Relevant Lines: 90939

💛 - Coveralls

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.

Thanks for doing this it's a great start! I left a few inline comments to start. The other thing which will be important here is to add some unit tests to make sure we exercise the new code path. You can add the tests to this file: https://github.com/Qiskit/qiskit/blob/main/test/c/test_circuit.c

@@ -378,6 +381,59 @@ pub struct OpCounts {
len: usize,
}

/// @ingroup QkCircuit
/// Append an arbitrary unitary matrix to the circuit.
/// The user passes a row-major array of interleaved real-imaginary pairs #[
Copy link
Member

Choose a reason for hiding this comment

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

Were you going to link to something here?

Comment on lines 413 to 420
let raw = unsafe { std::slice::from_raw_parts(matrix, dim * dim * 2) };
let data: Vec<Complex64> = raw
.chunks_exact(2)
.map(|x| Complex64::new(x[0], x[1]))
.collect();

// Build ndarray::Array2
let mat = Array2::from_shape_vec((dim, dim), data).expect("Invalid shape for unitary matrix");
Copy link
Member

@mtreinish mtreinish May 9, 2025

Choose a reason for hiding this comment

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

I wonder if using ArrayView::from_shape_ptr() here to create a view of the C array and then doing to_owned() to create a copy of it would be better. I guess the advantage of doing the ArrayView path is that it lets you control the striding too and support various memory layouts. While doing it like this explicit memory layout for what we store in Rust for the UnitaryGate. TBH, I'm fine either way as long as we document it, but I wanted to raise it as a question to discuss at least.

@mtreinish mtreinish moved this to PR open / Contributor working on it in Contributor Monitoring May 9, 2025
@mtreinish mtreinish added this to the 2.1.0 milestone May 9, 2025
@mtreinish mtreinish added Changelog: None Do not include in changelog C API Related to the C API labels May 9, 2025
maximizemaxwell and others added 7 commits May 9, 2025 21:46
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
@maximizemaxwell
Copy link
Contributor Author

Could u help me writing test codes?

@Cryoris
Copy link
Contributor

Cryoris commented May 13, 2025

Sure, we can help push this over the finish line 👍🏻 I had one other question; is there a reason you picked pairs of double instead of just complex numbers? We can use QkComplex64 which represents a complex double.

@maximizemaxwell
Copy link
Contributor Author

No, it can be removed and it would be better to replace it with QkComplex64..

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 now, just some small nits inline. I'll apply the suggestions and then approve this.

mtreinish
mtreinish previously approved these changes May 28, 2025
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.

Thanks for all the work on this!

@mtreinish mtreinish enabled auto-merge May 28, 2025 14:21
@mtreinish mtreinish added this pull request to the merge queue May 28, 2025
Merged via the queue into Qiskit:main with commit 25a34b0 May 28, 2025
20 of 26 checks passed
@github-project-automation github-project-automation bot moved this from PR open / Contributor working on it to Done in Contributor Monitoring May 28, 2025
jakelishman added a commit to jakelishman/qiskit-terra that referenced this pull request May 28, 2025
jakelishman added a commit to jakelishman/qiskit-terra that referenced this pull request May 28, 2025
This broke the C API tests but somehow made it through CI.  We need teh
CI fix, and to send the PR back to review.

This reverts commit 25a34b0.
github-merge-queue bot pushed a commit that referenced this pull request May 28, 2025
This broke the C API tests but somehow made it through CI.  We need teh
CI fix, and to send the PR back to review.

This reverts commit 25a34b0.
@jakelishman
Copy link
Member

@maximizemaxwell: I had to temporarily revert this PR because it was incompatible with #14340 that happened to merge right before it, and our CI didn't stop it from merging to main. It wasn't your fault - you couldn't possibly have known (it wasn't even on main), and it's our fault that CI merged it anyway.

Would you like to open a new PR from the same branch, and finalise the fixes that are needed to make it compatible?

mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request May 28, 2025
We originally merged this C API function in Qiskit#14334 but it had to be
reverted in Qiskit#14492 because the C API tests conflicted with Qiskit#14340 and
failed the tests. It was only able to merge because of a brief
configuration issue with CI. This commit reapplies the originally PR and
fixes the test conflict.

This reverts commit 49874c6.
@maximizemaxwell
Copy link
Contributor Author

Sure I'll check and finish soooon

Copy link
Contributor

mergify bot commented May 29, 2025

⚠️ The sha of the head commit of this PR conflicts with #14499. Mergify cannot evaluate rules on this PR. ⚠️

@maximizemaxwell
Copy link
Contributor Author

maximizemaxwell commented May 29, 2025

Has this issue been resolved?

@Cryoris
Copy link
Contributor

Cryoris commented May 29, 2025

You have to open a new PR and update the branch from main, to pull in the latest changes. What you started in #14499 looked correct, you just need to update the branch and update the code to use the new QkComplex64 format for complex numbers. I can help / take over with that, if you need any help 🙂

mtreinish added a commit to mtreinish/qiskit-core that referenced this pull request May 30, 2025
We originally merged this C API function in Qiskit#14334 but it had to be
reverted in Qiskit#14492 because the C API tests conflicted with Qiskit#14340 and
failed the tests. It was only able to merge because of a brief
configuration issue with CI. This commit reapplies the originally PR and
fixes the test conflict.

This reverts commit 49874c6.
github-merge-queue bot pushed a commit that referenced this pull request Jun 2, 2025
* Reapply "Add ``qk_circuit_unitary``  (#14334)" (#14492)

We originally merged this C API function in #14334 but it had to be
reverted in #14492 because the C API tests conflicted with #14340 and
failed the tests. It was only able to merge because of a brief
configuration issue with CI. This commit reapplies the originally PR and
fixes the test conflict.

This reverts commit 49874c6.

* Add tests for different sized matrices
rahaman-quantum pushed a commit to rahaman-quantum/qiskit that referenced this pull request Jun 20, 2025
* add initial qk_circuit_unitary

* add dependencies

* add dependencies in Cargo.toml

* changed circuit pushing logic

* fixed unsafe pointer

* fixed compile error

* Update circuit.rs

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

* Update circuit.rs

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

* Update crates/cext/src/circuit.rs

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

* initial version of test code

* changed order, patterns

* replaced double to QkComplex64

* accomodate windows and ubuntu nits

* clippppppppppy

* add check_input

* Use nalgebra for 1/2q cases

* fix docs

* changed to iter

* fixed docs

* Apply suggestions from code review

* Fix missing /// in docstring

---------

Co-authored-by: Matthew Treinish <mtreinish@kortar.org>
Co-authored-by: Julien Gacon <jules.gacon@googlemail.com>
rahaman-quantum pushed a commit to rahaman-quantum/qiskit that referenced this pull request Jun 20, 2025
This broke the C API tests but somehow made it through CI.  We need teh
CI fix, and to send the PR back to review.

This reverts commit 25a34b0.
rahaman-quantum pushed a commit to rahaman-quantum/qiskit that referenced this pull request Jun 20, 2025
…iskit#14509)

* Reapply "Add ``qk_circuit_unitary``  (Qiskit#14334)" (Qiskit#14492)

We originally merged this C API function in Qiskit#14334 but it had to be
reverted in Qiskit#14492 because the C API tests conflicted with Qiskit#14340 and
failed the tests. It was only able to merge because of a brief
configuration issue with CI. This commit reapplies the originally PR and
fixes the test conflict.

This reverts commit 49874c6.

* Add tests for different sized matrices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C API Related to the C API Changelog: None Do not include in changelog Community PR PRs from contributors that are not 'members' of the Qiskit repo
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Add C API interface for adding a UnitaryGate to a circuit
7 participants