-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add qk_obs_apply_layout
plus necessary tooling
#14106
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
Cryoris
wants to merge
4
commits into
Qiskit:main
Choose a base branch
from
Cryoris:qk-layout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+262
−13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// This code is part of Qiskit. | ||
// | ||
// (C) Copyright IBM 2025 | ||
// | ||
// This code is licensed under the Apache License, Version 2.0. You may | ||
// obtain a copy of this license in the LICENSE.txt file in the root directory | ||
// of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
// | ||
// Any modifications or derivative works of this code must retain this | ||
// copyright notice, and modified files need to carry a notice indicating | ||
// that they have been altered from the originals. | ||
|
||
use super::sparse_observable::check_ptr; | ||
use qiskit_accelerate::nlayout::{NLayout, PhysicalQubit}; | ||
|
||
/// @ingroup QkLayout | ||
/// Create a layout object from a virtual-to-physical qubit map. | ||
/// | ||
/// @param virt_to_phys A pointer to an array of length ``len``, defining the mapping | ||
/// from virtual to physical qubits. A null-pointer is not allowed. | ||
/// @param len The length of the above array. | ||
/// | ||
/// @return The layout. | ||
/// | ||
/// # Example | ||
/// | ||
/// uint32_t virt_to_phys[5] = {0, 2, 3, 4, 1}; | ||
/// QkLayout layout = qk_layout_new(virt_to_phys, 5); | ||
/// | ||
/// # Safety | ||
/// | ||
/// Behavior is undefined if ``virt_to_phys`` is not a non-null pointer to an array of ``uint32_t``, | ||
/// readable for ``len`` elements. | ||
#[no_mangle] | ||
#[cfg(feature = "cbinding")] | ||
pub unsafe extern "C" fn qk_layout_new(virt_to_phys: *const u32, len: usize) -> *mut NLayout { | ||
check_ptr(virt_to_phys).unwrap(); | ||
|
||
// SAFETY: The pointer is non-null and aligned and the user promises it is readable | ||
// for the required length. | ||
let data = unsafe { ::std::slice::from_raw_parts(virt_to_phys, len) }; | ||
|
||
let vector = data.iter().map(|q| PhysicalQubit(*q)).collect(); | ||
let layout = NLayout::from_virtual_to_physical(vector); | ||
|
||
Box::into_raw(Box::new(layout)) | ||
} | ||
|
||
/// @ingroup QkLayout | ||
/// Free the layout object. | ||
/// | ||
/// @param obs A pointer to the layout to free. | ||
/// | ||
/// # Example | ||
/// | ||
/// uint32_t virt_to_phys[5] = {0, 2, 3, 4, 1}; | ||
/// QkLayout layout = qk_layout_new(virt_to_phys, 5); | ||
/// qk_layout_free(layout); | ||
/// | ||
/// # Safety | ||
/// | ||
/// Behavior is undefined if ``layout`` is not either null or a valid pointer to a | ||
/// [NLayout]. | ||
#[no_mangle] | ||
#[cfg(feature = "cbinding")] | ||
pub unsafe extern "C" fn qk_layout_free(layout: *mut NLayout) { | ||
if !layout.is_null() { | ||
if !layout.is_aligned() { | ||
panic!("Attempted to free a non-aligned pointer.") | ||
} | ||
|
||
// SAFETY: We have verified the pointer is non-null and aligned, so it should be | ||
// readable by Box. | ||
unsafe { | ||
let _ = Box::from_raw(layout); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ use std::ffi::{c_char, CString}; | |
use crate::exit_codes::{CInputError, ExitCode}; | ||
use num_complex::Complex64; | ||
|
||
use qiskit_accelerate::nlayout::NLayout; | ||
use qiskit_accelerate::sparse_observable::{BitTerm, SparseObservable, SparseTermView}; | ||
|
||
#[cfg(feature = "python_binding")] | ||
|
@@ -68,7 +69,7 @@ impl TryFrom<&CSparseTerm> for SparseTermView<'_> { | |
} | ||
|
||
/// Check the pointer is not null and is aligned. | ||
fn check_ptr<T>(ptr: *const T) -> Result<(), CInputError> { | ||
pub(crate) fn check_ptr<T>(ptr: *const T) -> Result<(), CInputError> { | ||
if ptr.is_null() { | ||
return Err(CInputError::NullPointerError); | ||
}; | ||
|
@@ -820,6 +821,58 @@ pub unsafe extern "C" fn qk_obs_equal( | |
obs.eq(other) | ||
} | ||
|
||
/// @ingroup QkObs | ||
/// Apply a [NLayout] to the observable. | ||
/// | ||
/// @param obs A pointer to the observable. | ||
/// @param layout A pointer to the layout. | ||
/// | ||
/// @return The observable with the layout applied, or null if the layout was incompatible | ||
/// with the observable. | ||
/// | ||
/// # Example | ||
/// | ||
/// QkObs *obs = qk_obs_zero(4); | ||
/// | ||
/// QkBitTerm bit_terms[3] = {QkBitTerm_X, QkBitTerm_Y, QkBitTerm_Z}; | ||
/// uint32_t qubits[3] = {1, 2, 3}; | ||
/// complex double coeff = 1; | ||
/// QkObsTerm term = {coeff, 3, bit_terms, qubits, 4}; | ||
/// | ||
/// int err = qk_obs_add_term(obs, &term); | ||
/// if (err != 0) { | ||
/// qk_obs_free(obs); | ||
/// return err; | ||
/// } | ||
/// | ||
/// uint32_t virt_to_phys[3] = {0, 3, 2, 1}; | ||
/// QkLayout *layout = qk_layout_new(virt_to_phys, num_qubits); | ||
/// QkObs *obs_with_layout = qk_obs_apply_layout(obs, layout); | ||
/// | ||
/// # Safety | ||
/// | ||
/// Behavior is undefined if ``obs`` is not a valid, non-null pointer to ``QkObs`` or if ``layout`` | ||
/// is not a valid, non-null pointer to ``QkLayout``. | ||
#[no_mangle] | ||
#[cfg(feature = "cbinding")] | ||
pub unsafe extern "C" fn qk_obs_apply_layout( | ||
obs: *const SparseObservable, | ||
layout: *const NLayout, | ||
) -> *mut SparseObservable { | ||
let obs = unsafe { const_ptr_as_ref(obs) }; | ||
let layout = unsafe { const_ptr_as_ref(layout) }; | ||
|
||
// Cast the slice of PhysicalQubit (which is u32) to plain u32, which is required | ||
// by the apply_layout method. | ||
let as_slice: &[u32] = ::bytemuck::cast_slice(&layout.virt_to_phys); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line (and the |
||
let obs_with_layout = match obs.apply_layout(Some(as_slice), obs.num_qubits()) { | ||
Ok(obs_with_layout) => obs_with_layout, | ||
Err(_) => return ::std::ptr::null_mut(), | ||
}; | ||
|
||
Box::into_raw(Box::new(obs_with_layout)) | ||
} | ||
|
||
/// @ingroup QkObs | ||
/// Return a string representation of a ``SparseObservable``. | ||
/// | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
features_c: | ||
- | | ||
Added ``qk_obs_apply_layout`` to apply a layout, in form of ``QkLayout``, to an | ||
observable. | ||
- | | ||
Introduce ``QkLayout`` to represent the mapping of virtual to physical qubits, | ||
along with the related functions ``qk_layout_new`` and ``qk_layout_free``. | ||
A layout can be constructed by passing an array containing the virtual-to-physical | ||
mapping into ``qk_layout_new``, for example | ||
|
||
.. code-block:: c | ||
|
||
QkObs *obs = qk_obs_zero(4); | ||
|
||
QkBitTerm bit_terms[3] = {QkBitTerm_X, QkBitTerm_Y, QkBitTerm_Z}; | ||
uint32_t qubits[3] = {1, 2, 3}; | ||
complex double coeff = 1; | ||
QkObsTerm term = {coeff, 3, bit_terms, qubits, 4}; | ||
|
||
qk_obs_add_term(obs, &term); | ||
|
||
uint32_t virt_to_phys[3] = {0, 3, 2, 1}; | ||
QkLayout *layout = qk_layout_new(virt_to_phys, num_qubits); | ||
QkObs *obs_with_layout = qk_obs_apply_layout(obs, layout); | ||
|
||
Ensure to free the layout at the end of the computation, via | ||
|
||
.. code-block:: c | ||
|
||
qk_layout_free(layout); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
These are made public to be able to easily call
SparseObservable::apply_layout
, which takes thevirt_to_phys
vector. This could be worked around by using the available public methods, but I think that would require a copy that I wanted to avoid.