Skip to content
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

Fix Relabel for odd size arrays #2082

Merged
merged 1 commit into from
Dec 30, 2024
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
23 changes: 15 additions & 8 deletions compiler/qsc_eval/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,14 @@ pub fn qubit_relabel(
}
(false, true) => {
// The right qubit has been relabeled, so we need to swap the left qubit with the
// qubit that the right qubit was relabeled to.
DmitryVasilevsky marked this conversation as resolved.
Show resolved Hide resolved
let mapped = *map
// new label for the right qubit.
let label = *map
.keys()
.find(|k| map[*k] == r)
.expect("mapped qubit should be present as both key and value");
swap(l, mapped);
swap(l, label);
map.insert(l, r);
map.insert(mapped, l);
map.insert(label, l);
}
(true, false) => {
// The left qubit has been relabeled, so we swap the qubits as normal but
Expand All @@ -410,14 +410,21 @@ pub fn qubit_relabel(
map.insert(r, mapped);
}
(true, true) => {
// Both qubits have been relabeled, so we need to swap the mapped right qubit with
// Both qubits have been relabeled, so we need to swap new label for the right qubit with
// the left qubit and remember the new mapping of both qubits.
// This is effectively a combination of the second and third cases above.
let label_r = *map
.keys()
.find(|k| map[*k] == r)
.expect("mapped qubit should be present as both key and value");
let mapped_l = *map.get(&l).expect("mapped qubit should be present");
let mapped_r = *map.get(&r).expect("mapped qubit should be present");

// This swap is only necessary if the labels don't already point to each other.
if mapped_l != r && mapped_r != l {
swap(mapped_r, l);
map.insert(mapped_r, mapped_l);
map.insert(l, r);
swap(label_r, l);
map.insert(label_r, mapped_l);
map.insert(l, mapped_r);
}
}
}
Expand Down
108 changes: 106 additions & 2 deletions library/src/tests/canon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,78 @@ fn check_relabel_rotational_permutation_alternate_expression() {
);
}

#[test]
fn check_relabel_rotational_permutation_size_4() {
test_expression(
"{
use qs = Qubit[4];
// Prepare |01+0⟩
X(qs[1]);
H(qs[2]);
Relabel(qs, qs[2...] + qs[0..1]);
// Expected state is |+001⟩, perform adjoint to get back to ground state.
H(qs[0]);
X(qs[Length(qs)-1]);
// Qubit release will fail if the state is not |000⟩
}",
&Value::unit(),
);
}

#[test]
fn check_relabel_rotational_permutation_size_5() {
test_expression(
"{
use qs = Qubit[5];
// Prepare |01+00⟩
X(qs[1]);
H(qs[2]);
Relabel(qs, qs[2...] + qs[0..1]);
// Expected state is |+0001⟩, perform adjoint to get back to ground state.
H(qs[0]);
X(qs[Length(qs)-1]);
// Qubit release will fail if the state is not |000⟩
}",
&Value::unit(),
);
}

#[test]
fn check_relabel_rotational_permutation_size_6() {
test_expression(
"{
use qs = Qubit[6];
// Prepare |01+000⟩
X(qs[1]);
H(qs[2]);
Relabel(qs, qs[2...] + qs[0..1]);
// Expected state is |+00001⟩, perform adjoint to get back to ground state.
H(qs[0]);
X(qs[Length(qs)-1]);
// Qubit release will fail if the state is not |000⟩
}",
&Value::unit(),
);
}

#[test]
fn check_relabel_rotational_permutation_size_7() {
test_expression(
"{
use qs = Qubit[7];
// Prepare |01+0000⟩
X(qs[1]);
H(qs[2]);
Relabel(qs, qs[2...] + qs[0..1]);
// Expected state is |+000001⟩, perform adjoint to get back to ground state.
H(qs[0]);
X(qs[Length(qs)-1]);
// Qubit release will fail if the state is not |000⟩
}",
&Value::unit(),
);
}

#[test]
fn check_relabel_four_qubit_shuffle_permutation() {
test_expression(
Expand All @@ -162,12 +234,44 @@ fn check_relabel_four_qubit_shuffle_permutation() {
// Prepare |01+i⟩
X(qs[1]);
H(qs[2]);
Y(qs[3]);
H(qs[3]);
S(qs[3]);
H(qs[3]);
Relabel([qs[0], qs[1], qs[2], qs[3]], [qs[1], qs[0], qs[3], qs[2]]);
// Expected state is |10i+⟩, perform adjoint to get back to ground state.
X(qs[0]);
Y(qs[2]);
H(qs[2]);
Adjoint S(qs[2]);
H(qs[2]);
H(qs[3]);
// Qubit release will fail if the state is not |0000⟩
}",
&Value::unit(),
);
}

#[test]
fn check_relabel_five_qubit_shuffle_permutation() {
test_expression(
"{
use qs = Qubit[5];
// Prepare |01+i-⟩
X(qs[1]);
H(qs[2]);
H(qs[3]);
S(qs[3]);
H(qs[3]);
H(qs[4]);
Z(qs[4]);
Relabel([qs[0], qs[1], qs[2], qs[3], qs[4]], [qs[1], qs[0], qs[3], qs[4], qs[2]]);
// Expected state is |10i-+⟩, perform adjoint to get back to ground state.
X(qs[0]);
H(qs[2]);
Adjoint S(qs[2]);
H(qs[2]);
Z(qs[3]);
H(qs[3]);
H(qs[4]);
// Qubit release will fail if the state is not |0000⟩
}",
&Value::unit(),
Expand Down
Loading