pub struct Layout { /* private fields */ }Expand description
Maps circuit logical qubits to physical qubits on a quantum device.
A layout owns the set of physical qubits available to a placement or routing step. Every logical qubit present in the layout has exactly one physical mapping. A physical qubit may be vacant.
Auxiliary qubit ownership and lifetime are intentionally outside this type.
From the layout perspective, an auxiliary logical qubit behaves like any
other logical qubit after it is explicitly Layout::binded.
Implementations§
Source§impl Layout
impl Layout
Sourcepub fn new(
logical: Vec<LogicalQubit>,
physical: Vec<PhysicalQubit>,
init_map: Option<BTreeMap<LogicalQubit, PhysicalQubit>>,
) -> Result<Self, LayoutError>
pub fn new( logical: Vec<LogicalQubit>, physical: Vec<PhysicalQubit>, init_map: Option<BTreeMap<LogicalQubit, PhysicalQubit>>, ) -> Result<Self, LayoutError>
Creates a layout and maps each supplied logical qubit to a physical qubit.
Entries in init_map are applied first. Remaining logical qubits are
mapped to remaining physical qubits in the order supplied by logical
and physical. Extra physical qubits remain vacant.
§Errors
Returns LayoutError if:
- there are more logical qubits than physical qubits;
- the logical or physical qubit list contains duplicate entries;
init_mapreferences an undeclared logical or physical qubit;init_mapmaps multiple logical qubits to one physical qubit.
§Example
use cqlib_core::device::{Layout, LogicalQubit, PhysicalQubit};
use std::collections::BTreeMap;
let logical = vec![LogicalQubit::new(0), LogicalQubit::new(1)];
let physical = vec![PhysicalQubit::new(100), PhysicalQubit::new(101)];
let mut init = BTreeMap::new();
init.insert(LogicalQubit::new(0), PhysicalQubit::new(101));
let layout = Layout::new(logical, physical, Some(init)).unwrap();
assert_eq!(
layout.get_physical(LogicalQubit::new(0)),
Some(PhysicalQubit::new(101)),
);Sourcepub fn from_pairs(
logical_physical: &[(u32, u32)],
physical_count: u32,
) -> Result<Self, LayoutError>
pub fn from_pairs( logical_physical: &[(u32, u32)], physical_count: u32, ) -> Result<Self, LayoutError>
Creates a layout from (logical, physical) qubit ID pairs.
Logical qubits are exactly the logical IDs that appear in
logical_physical. Physical qubits are 0..physical_count; any
physical qubit not referenced by a pair remains vacant.
§Errors
Returns LayoutError if a logical ID appears more than once, a
physical ID appears more than once, or any physical ID is outside
0..physical_count.
§Example
use cqlib_core::device::{Layout, LogicalQubit, PhysicalQubit};
let layout = Layout::from_pairs(&[(0, 2), (1, 0)], 4).unwrap();
assert_eq!(
layout.get_physical(LogicalQubit::new(0)),
Some(PhysicalQubit::new(2)),
);
assert_eq!(layout.num_vacant_physical(), 2);Sourcepub fn num_logical(&self) -> usize
pub fn num_logical(&self) -> usize
Returns the number of mapped logical qubits.
Sourcepub fn num_physical(&self) -> usize
pub fn num_physical(&self) -> usize
Returns the number of physical qubits available to the layout.
Sourcepub fn num_vacant_physical(&self) -> usize
pub fn num_vacant_physical(&self) -> usize
Returns the number of physical qubits that do not carry a logical qubit.
Sourcepub fn get_physical(&self, logical: LogicalQubit) -> Option<PhysicalQubit>
pub fn get_physical(&self, logical: LogicalQubit) -> Option<PhysicalQubit>
Returns the physical qubit carrying logical, if it is bound.
Sourcepub fn get_logical(&self, physical: PhysicalQubit) -> Option<LogicalQubit>
pub fn get_logical(&self, physical: PhysicalQubit) -> Option<LogicalQubit>
Returns the logical qubit carried by physical, if the position is occupied.
Sourcepub fn logical_qubits(&self) -> impl Iterator<Item = LogicalQubit> + '_
pub fn logical_qubits(&self) -> impl Iterator<Item = LogicalQubit> + '_
Returns an iterator over mapped logical qubits.
Sourcepub fn physical_qubits(&self) -> impl Iterator<Item = PhysicalQubit> + '_
pub fn physical_qubits(&self) -> impl Iterator<Item = PhysicalQubit> + '_
Returns an iterator over physical qubits available to the layout.
Sourcepub fn vacant_physical_qubits(&self) -> impl Iterator<Item = PhysicalQubit> + '_
pub fn vacant_physical_qubits(&self) -> impl Iterator<Item = PhysicalQubit> + '_
Returns an iterator over vacant physical qubits.
Sourcepub fn is_physical_vacant(&self, physical: PhysicalQubit) -> bool
pub fn is_physical_vacant(&self, physical: PhysicalQubit) -> bool
Returns whether physical belongs to the layout and is vacant.
Sourcepub fn l2p_map(&self) -> &BTreeMap<LogicalQubit, PhysicalQubit>
pub fn l2p_map(&self) -> &BTreeMap<LogicalQubit, PhysicalQubit>
Returns the logical-to-physical qubit mapping.
Sourcepub fn p2l_map(&self) -> &BTreeMap<PhysicalQubit, LogicalQubit>
pub fn p2l_map(&self) -> &BTreeMap<PhysicalQubit, LogicalQubit>
Returns the physical-to-logical qubit mapping.
Sourcepub fn bind(
&mut self,
logical: LogicalQubit,
physical: PhysicalQubit,
) -> Result<(), LayoutError>
pub fn bind( &mut self, logical: LogicalQubit, physical: PhysicalQubit, ) -> Result<(), LayoutError>
Binds an unmapped logical qubit to a vacant physical qubit.
This operation may introduce a new logical qubit to the layout. The caller is responsible for ensuring that the logical qubit exists in the circuit and is registered with the compiler resource manager when required.
§Errors
Returns LayoutError::InvalidPhysicalQubit if physical does not
belong to the layout. Returns LayoutError::LogicalQubitAlreadyBound
or LayoutError::PhysicalQubitAlreadyOccupied if either qubit already
participates in a mapping.
Sourcepub fn unbind(
&mut self,
logical: LogicalQubit,
) -> Result<PhysicalQubit, LayoutError>
pub fn unbind( &mut self, logical: LogicalQubit, ) -> Result<PhysicalQubit, LayoutError>
Removes the mapping for logical and returns the released physical qubit.
Sourcepub fn swap_physical(
&mut self,
phys_a: PhysicalQubit,
phys_b: PhysicalQubit,
) -> Result<(), LayoutError>
pub fn swap_physical( &mut self, phys_a: PhysicalQubit, phys_b: PhysicalQubit, ) -> Result<(), LayoutError>
Swaps the logical qubits carried by two physical qubits.
Either physical qubit may be vacant. Swapping an occupied qubit with a vacant qubit moves the logical qubit to the vacant position.
§Errors
Returns LayoutError::InvalidPhysicalQubit if either physical qubit
does not belong to the layout.
Trait Implementations§
impl Eq for Layout
impl StructuralPartialEq for Layout
Auto Trait Implementations§
impl Freeze for Layout
impl RefUnwindSafe for Layout
impl Send for Layout
impl Sync for Layout
impl Unpin for Layout
impl UnsafeUnpin for Layout
impl UnwindSafe for Layout
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DistributionExt for Twhere
T: ?Sized,
impl<T> DistributionExt for Twhere
T: ?Sized,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.impl<T, U> Imply<T> for U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.