Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions synir/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,39 @@ pub trait MaskedPropagateClifford
where
Self: Sized,
{
fn masked_cx(&self, control: IndexType, target: IndexType, mask: &BitVec) -> &Self;
fn masked_s(&self, target: IndexType, mask: &BitVec) -> &Self;
fn masked_v(&self, target: IndexType, mask: &BitVec) -> &Self;
fn masked_cx(&mut self, control: IndexType, target: IndexType, mask: &BitVec) -> &mut Self;
fn masked_s(&mut self, target: IndexType, mask: &BitVec) -> &mut Self;
fn masked_v(&mut self, target: IndexType, mask: &BitVec) -> &mut Self;

fn masked_s_dgr(&self, target: IndexType, mask: &BitVec) -> &Self {
fn masked_s_dgr(&mut self, target: IndexType, mask: &BitVec) -> &mut Self {
self.masked_z(target, mask).masked_s(target, mask)
}

fn masked_v_dgr(&self, target: IndexType, mask: &BitVec) -> &Self {
fn masked_v_dgr(&mut self, target: IndexType, mask: &BitVec) -> &mut Self {
self.masked_x(target, mask).masked_v(target, mask)
}

fn masked_x(&self, target: IndexType, mask: &BitVec) -> &Self {
fn masked_x(&mut self, target: IndexType, mask: &BitVec) -> &mut Self {
self.masked_v(target, mask).masked_v(target, mask)
}

fn masked_y(&self, target: IndexType, mask: &BitVec) -> &Self {
fn masked_y(&mut self, target: IndexType, mask: &BitVec) -> &mut Self {
self.masked_s_dgr(target, mask)
.masked_x(target, mask)
.masked_s(target, mask)
}

fn masked_z(&self, target: IndexType, mask: &BitVec) -> &Self {
fn masked_z(&mut self, target: IndexType, mask: &BitVec) -> &mut Self {
self.masked_s(target, mask).masked_s(target, mask)
}

fn masked_h(&self, target: IndexType, mask: &BitVec) -> &Self {
fn masked_h(&mut self, target: IndexType, mask: &BitVec) -> &mut Self {
self.masked_s(target, mask)
.masked_v(target, mask)
.masked_s(target, mask)
}

fn masked_cz(&self, control: IndexType, target: IndexType, mask: &BitVec) -> &Self {
fn masked_cz(&mut self, control: IndexType, target: IndexType, mask: &BitVec) -> &mut Self {
self.masked_h(target, mask);
self.masked_cx(control, target, mask);
self.masked_h(target, mask)
Expand Down
38 changes: 18 additions & 20 deletions synir/src/data_structures/clifford_tableau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,20 @@ impl CliffordTableau {
/// Implements algorithms from https://doi.org/10.22331/q-2022-06-13-734 and Qiskit Clifford implementation
pub(crate) fn prepend(&self, lhs: &Self) -> Self {
let size = self.size();
let pauli_columns = vec![PauliString::from_text(&"I".repeat(2 * size)); size];
let mut pauli_columns = vec![PauliString::from_text(&"I".repeat(2 * size)); size];
// Matrix-multiplication for M(rhs o self) = M(self) * M(rhs) as this is a row-permutation.
// Loop re-order to be (k, i, j) as j ordering is contiguous.
for (k, rhs_pauli_column) in self.pauli_columns.iter().enumerate() {
for i in 0..size {
let mut x = pauli_columns[k].x.write().unwrap();
let mut z = pauli_columns[k].z.write().unwrap();
*x ^= BitVec::repeat(rhs_pauli_column.x(i), 2 * size)
& lhs.pauli_columns[i].x.read().unwrap().as_bitslice();
let column = &mut pauli_columns[k];
let x = &mut column.x;
let z = &mut column.z;
*x ^= BitVec::repeat(rhs_pauli_column.x(i), 2 * size) & &lhs.pauli_columns[i].x;
*x ^= BitVec::repeat(rhs_pauli_column.x(i + size), 2 * size)
& lhs.pauli_columns[i].z.read().unwrap().as_bitslice();
*z ^= BitVec::repeat(rhs_pauli_column.z(i), 2 * size)
& lhs.pauli_columns[i].x.read().unwrap().as_bitslice();
& &lhs.pauli_columns[i].z;
*z ^= BitVec::repeat(rhs_pauli_column.z(i), 2 * size) & &lhs.pauli_columns[i].x;
*z ^= BitVec::repeat(rhs_pauli_column.z(i + size), 2 * size)
& lhs.pauli_columns[i].z.read().unwrap().as_bitslice();
& &lhs.pauli_columns[i].z;
}
}

Expand Down Expand Up @@ -139,10 +138,8 @@ impl CliffordTableau {
// Contribution of combination of signs in rhs basis.
// Calculate matrix vector M(lhs) * sign(rhs)
for (j, lhs_pauli_column) in lhs.pauli_columns.iter().enumerate() {
new_signs ^= BitVec::repeat(self.signs[j], 2 * size)
& lhs_pauli_column.x.read().unwrap().as_bitslice();
new_signs ^= BitVec::repeat(self.signs[j + size], 2 * size)
& lhs_pauli_column.z.read().unwrap().as_bitslice();
new_signs ^= BitVec::repeat(self.signs[j], 2 * size) & &lhs_pauli_column.x;
new_signs ^= BitVec::repeat(self.signs[j + size], 2 * size) & &lhs_pauli_column.z;
}

// Get rid of `i` factors and convert to sign flips
Expand Down Expand Up @@ -185,7 +182,7 @@ impl HasAdjoint for CliffordTableau {
let size = self.size();
// Create new CliffordTableau entries

let new_columns = vec![PauliString::from_text(&"I".repeat(2 * size)); size];
let mut new_columns = vec![PauliString::from_text(&"I".repeat(2 * size)); size];
(0..size).for_each(|i| {
for (j, pauli_column) in self.pauli_columns.iter().enumerate() {
let ((x1, z1), (x2, z2)) = reverse_flow(
Expand All @@ -195,8 +192,9 @@ impl HasAdjoint for CliffordTableau {
pauli_column.z(i + size),
);

let mut x = new_columns[i].x.write().unwrap();
let mut z = new_columns[i].z.write().unwrap();
let new_column = &mut new_columns[i];
let x = &mut new_column.x;
let z = &mut new_column.z;
x.replace(j, x1);
z.replace(j, z1);
x.replace(j + size, x2);
Expand Down Expand Up @@ -250,10 +248,10 @@ impl PropagateClifford for CliffordTableau {
.unwrap();

let mut scratch = BitVec::repeat(true, 2 * n);
scratch ^= target.x.read().unwrap().as_bitslice();
scratch ^= control.z.read().unwrap().as_bitslice();
scratch &= control.x.read().unwrap().as_bitslice();
scratch &= target.z.read().unwrap().as_bitslice();
scratch ^= &target.x;
scratch ^= &control.z;
scratch &= &control.x;
scratch &= &target.z;
self.signs ^= scratch;

cx(control, target);
Expand Down
Loading
Loading