microsoft/qdk
Publicmirrored from https://github.com/microsoft/qdkAvailable
source/paulimer/src/pauli/dense.rs
61lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | use crate::quantum_core::PositionedPauliObservable; |
| 5 | |
| 6 | use super::{PauliUnitaryProjective, SparsePauli}; |
| 7 | use crate::{ |
| 8 | bits::BitVec, |
| 9 | pauli::{generic::PauliUnitary, Pauli, PauliBinaryOps}, |
| 10 | NeutralElement, |
| 11 | }; |
| 12 | pub type DensePauli = PauliUnitary<BitVec, u8>; |
| 13 | pub type DensePauliProjective = PauliUnitaryProjective<BitVec>; |
| 14 | |
| 15 | impl From<&[PositionedPauliObservable]> for DensePauli { |
| 16 | fn from(value: &[PositionedPauliObservable]) -> Self { |
| 17 | let r: SparsePauli = value.into(); |
| 18 | match super::Pauli::max_qubit_id(&r) { |
| 19 | Some(max_id) => { |
| 20 | let mut dense = |
| 21 | <DensePauli as crate::NeutralElement>::neutral_element_of_size(max_id + 1); |
| 22 | super::PauliBinaryOps::assign(&mut dense, &r); |
| 23 | dense |
| 24 | } |
| 25 | None => <DensePauli as crate::NeutralElement>::default_size_neutral_element(), |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | impl<const LENGTH: usize> From<[PositionedPauliObservable; LENGTH]> for DensePauli { |
| 31 | fn from(pauli_observable: [PositionedPauliObservable; LENGTH]) -> Self { |
| 32 | pauli_observable.as_slice().into() |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl<const LENGTH: usize> From<&[PositionedPauliObservable; LENGTH]> for DensePauli { |
| 37 | fn from(pauli_observable: &[PositionedPauliObservable; LENGTH]) -> Self { |
| 38 | pauli_observable.as_slice().into() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl From<Vec<PositionedPauliObservable>> for DensePauli { |
| 43 | fn from(value: Vec<PositionedPauliObservable>) -> Self { |
| 44 | value.as_slice().into() |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | impl From<&Vec<PositionedPauliObservable>> for DensePauli { |
| 49 | fn from(value: &Vec<PositionedPauliObservable>) -> Self { |
| 50 | value.as_slice().into() |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub fn dense_from<PauliLike: Pauli>(pauli: &PauliLike, qubit_count: usize) -> DensePauli |
| 55 | where |
| 56 | DensePauli: PauliBinaryOps<PauliLike>, |
| 57 | { |
| 58 | let mut result = DensePauli::neutral_element_of_size(qubit_count); |
| 59 | result.assign(pauli); |
| 60 | result |
| 61 | } |
| 62 | |