-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Initial Target
C API
#14248
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
Initial Target
C API
#14248
Conversation
Pull Request Test Coverage Report for Build 15447438495Warning: 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
💛 - Coveralls |
- Add provisional `PropertyMap` to represent the mapping of `qargs : InstructionProperties` before adding it to the `Target`. - `InstructionProperties`: - free. - `Target`: - `update_instruction_prop` - `operation_names` - `physical_qubits` - `non_global_operation_names`. - `operation_names_for_qargs`. - `qargs_for_operation_names`. - `qargs`. - `instruction_supported`. - `contains_key`.
- Add getter methods for `InstructionProperties`. - Add `length` method for `PropertyMap`. - Add name maping for `PropertyMap`.
- Add `get` method to `Target`. - Add missing methods to `PropertyMap`. - Fix methods returning arrays to C by adding a a forget statement to prevent pointer from being destroyed. - Add getter methods to the `C` api of target. - Add helper function to parse qargs.
- Move `print_qargs` helper to top of module.
- Add helper method to compare arrays. - Fix print method for qargs.
- Use correct type of pointers for outputs,
- Use IndexSet to preserve insertion order to avoid sorting in the C API.
- Sort the string arrays obtained from the target in C during tests. - Remove `Copy` from `InstructionProperties`. - Add helper method to sort string array outputs, coming from the Target.
- Add getters for `description`, `dt` `min_length` `granularity`, `acquire_alignment`, and `pulse_alignment`. - Add Target construction test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this isn't ready for review yet, and I haven't done a full review. But I opened this up and got triggered on the first c function so left a comment there.
- Add setters for `QkTarget` attributes.
crates/cext/src/transpiler/target.rs
Outdated
/// non-null pointer to a ``QkTargetEntry`` object. | ||
#[no_mangle] | ||
#[cfg(feature = "cbinding")] | ||
pub unsafe extern "C" fn qk_target_entry_len(entry: *const TargetEntry) -> usize { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also here, could we call it something that reflects the property better, like num_instructions
or something? 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For an entry, I think the current name is valid since it is the number of both qargs and properties (we could get away with calling it qk_target_entry_num_props
or something among those lines). This name, however, could work for the qk_target_len
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in a35f1bb
Co-authored-by: Julien Gacon <gaconju@gmail.com>
- Rename `qk_target_len` to `qk_target_num_instructions`. - Rename `qk_target_entry_len` to `qk_target_num_properties`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm very happy with the code, but the docs need some more attention (and I think we can complete the reno now 🙂). I also left a comment on the tests 🙂
crates/cext/src/transpiler/target.rs
Outdated
|
||
use crate::exit_codes::ExitCode; | ||
use crate::pointers::{const_ptr_as_ref, mut_ptr_as_ref}; | ||
use core::f64; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a question: is this the same as std::f64
? I'm surprised we need an import to use the native type 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't need to import this from core
. We're not in a no-std build for rust we very explicitly rely on the stdlib in cext (and qiskit more broadly). You should just be able to use the built in f64 intrinsic. If that isn't sufficient you might want to use std::ffi::c_double
but I don't think think that should be necessary.
features_transpiler: | ||
- | | ||
Support for creation, manipulation, and interaction of :class:`~.Target` have | ||
been added to the Qiskit C API. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are happy now I think 🙂
- Use `u32` for `Target` length and `min_length`. Co-authored-by: Julien Gacon <gaconju@gmail.com>
#14526 should fix the CI failures |
- Fix indentation in helper function docstrings in target.rs
Co-authored-by: Julien Gacon <gaconju@gmail.com>
- `qk_target_entry_add_property` will throw an error code if the wrong qargs number is ever provided. - Add comparison of error codes in unittests.
Co-authored-by: Julien Gacon <gaconju@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM thanks for all the work -- I believe we're in very good shape now and have a API that works well for C space. The true test will come once users will want to build custom targets (assuming that auto-generated targets from backends will be generated in Rust) 🙂
* Initial: Enable a C API to build and modify a Target * Add: `add_instruction` function * Add: More methods to `Target` C API - Add provisional `PropertyMap` to represent the mapping of `qargs : InstructionProperties` before adding it to the `Target`. - `InstructionProperties`: - free. - `Target`: - `update_instruction_prop` - `operation_names` - `physical_qubits` - `non_global_operation_names`. - `operation_names_for_qargs`. - `qargs_for_operation_names`. - `qargs`. - `instruction_supported`. - `contains_key`. * Add: C unit test - Add getter methods for `InstructionProperties`. - Add `length` method for `PropertyMap`. - Add name maping for `PropertyMap`. * Fix: Add `IndexMap` and `Ahash` dependencies to qiskit_cext. - Add `get` method to `Target`. - Add missing methods to `PropertyMap`. - Fix methods returning arrays to C by adding a a forget statement to prevent pointer from being destroyed. - Add getter methods to the `C` api of target. - Add helper function to parse qargs. * Test: Add tests to update properties and get non global names. * Add `operation_names_for_qargs` test case. - Move `print_qargs` helper to top of module. * Add: `qargs_for_operation_names` test case - Add helper method to compare arrays. - Fix print method for qargs. * Docs: Add first wave of docstrings. * Fix: Incorrect typing in `test_target.c` * Fix: Add missing docstrings. - Use correct type of pointers for outputs, * Lint: Fix c-format * Fix: Remove option from `qarg_gate_map`. - Use IndexSet to preserve insertion order to avoid sorting in the C API. * Test: Add `qargs` test. * FIx: Add missing docstrings for remainign tests * Fix: Type errors in MacOS * Fix: Undo extra changes in the target. - Sort the string arrays obtained from the target in C during tests. - Remove `Copy` from `InstructionProperties`. - Add helper method to sort string array outputs, coming from the Target. * Add: more complete constructor for `Target`. - Add getters for `description`, `dt` `min_length` `granularity`, `acquire_alignment`, and `pulse_alignment`. - Add Target construction test. * Fix: Clean up pointers in `qk_target_new` - Add setters for `QkTarget` attributes. * Fix: Add release note * Fix: Typos in docstring * Lint: Fix lint for C * Fix: Use structs for collections, specify length. - Introduce `QkTargetQargs`, `QkTargetNameList`, and `QkTargetQargsList` to expose collections of operation names and qargs to C exposing the length of the collection. * Fix: Use ``Target`` for docstrings. * Fix: Use the `qiskit_transpiler` crate. * Fix: Add "qiskit-transpiler" to cbindgen.toml - Remove incorrect usage of pointers for `qk_target_dt`. * Fix: Incorrect import of `PhysicalQubit` in target.rs * Fix: Remove usage of `string` to query instructions, use `StandardGate` instead. - Introduce `Target` specific exitcodes starting at 300. - Change methods to use `StandardGate` instead of String. * Fix: Remove `description` from the C API. * FIx: Remove `InstructionProperties` from the C API - Introduce `QkInstProperties` as a C representable variant of `InstructionProperties`. - Move `InstructionProperties` instantiation to function calls. Now you can directly send duration or error as function arguments. - Make `PropsMap` be either initialized or `NULL`. * Fix: Incorrect documentation usage for `qk_target_new()` * Fix: Use NAN in one of the examples. * Docs: Add documentation for the `Target` C API * Lint: Qiskit header format in docs * Fix: Use `NaN` when `dt` is not set. * Refactor: Remove all querrying methods from Target, PropsMap. - Remove all helper structs. - All removed changes will be spun off to a different PR. * Fix: Implement `From<TargetError>` for `ErrorCode`. * Add: Separate method to add gates with fixed params. - Since parameters are not fully supported in C as of yet, we have to separate the methods to make a clear division between how to add a gate with/without parameters, and making additions with fixed parameters fully explicit in the Target. * Docs: Address new changes in documentation * Fix: Add a way of copying a Target from C. * Fix: Address review comments Co-authored-by: Julien Gacon <gaconju@gmail.com> * Lint: Fix formatting * Remove: `Target::get` method. - Will be re-added in future updates. * Docs: Remove volatility line from `PropsMap` docs. * Refactor: `QkPropsMap` is now `QkTargetEntry`. - Add rust struct `TargetEntry` to better represent adding a gate to the Target from C, - Add `SmallVec` as a dependency. - Remove `qk_target_add_instruction_fixed` in favor of performing the checks during the creation of the `QkTargetEntry`. - Update documentation with newer additions. * Docs: update documentation to clearly * Apply suggestions from code review Co-authored-by: Julien Gacon <gaconju@gmail.com> * Chore: Rename len methods. - Rename `qk_target_len` to `qk_target_num_instructions`. - Rename `qk_target_entry_len` to `qk_target_num_properties`. * Chore: Fix documentation further - Use `u32` for `Target` length and `min_length`. Co-authored-by: Julien Gacon <gaconju@gmail.com> * Docs: Add example to release note * Chore: Remove unused error codes * Fix: Remove conversion in vf2 vf2_layout - Fix indentation in helper function docstrings in target.rs * Apply suggestions from code review Co-authored-by: Julien Gacon <gaconju@gmail.com> * Fix: Make `qk_target_entry_add_property` fallible. - `qk_target_entry_add_property` will throw an error code if the wrong qargs number is ever provided. - Add comparison of error codes in unittests. * Update docs/cdoc/qk-target-entry.rst Co-authored-by: Julien Gacon <gaconju@gmail.com> * Fix: Check pointer before consuming in `qk_target_add_instruction`. * Chore: Clean up the target tests. * Fix: More mistakes in tests. --------- Co-authored-by: Julien Gacon <gaconju@gmail.com>
Summary
Fixes #14241 and #14376
The following commits enable a C FFI for creation of a Rust
Target
.Details and comments
As we work into expanding Qiskit into working in C, it is important to bring parts of the transpiler into this domain. One such part is the
Target
, which contains a lot of information about the hardware a circuit will be executed in. The following commits expose a C FFI that allows us to create and interact with a Target directly from C.In order to achieve this we have 3 types of pointers being exposed to C:
QkTarget
- which points to aTarget
in Rust.QkPropsMap
- which represents a mapping betweenQargs
:InstructionProperties
, or, in C terms, this would be a mapping ofuint32_t[]
:QkInstructionProps
.The C FFI should allow us to:
QkGate
, a list of parametersdouble[]
, and an optionalQkPropsMap
containing the instruction properties.QkTarget
instruction's properties after being added.