Skip to content

Commit

Permalink
Fix PauliString.before reset not working (#792)
Browse files Browse the repository at this point in the history
- Also switch `circuit.insert` to doing auto-fusion of operations
  • Loading branch information
Strilanc authored Jul 3, 2024
1 parent cfcf163 commit e0d99eb
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 32 deletions.
10 changes: 4 additions & 6 deletions doc/python_api_reference_vDev.md
Original file line number Diff line number Diff line change
Expand Up @@ -2236,9 +2236,8 @@ def insert(
) -> None:
"""Inserts an operation at the given index, pushing existing operations forward.
Note that, unlike when appending operations or parsing stim circuit files,
inserted operations aren't automatically fused into the preceding operation.
This is to avoid creating complicated situations where it's difficult to reason
Beware that inserted operations are automatically fused with the preceding
and following operations, if possible. This can make it complex to reason
about how the indices of operations change in response to insertions.
Args:
Expand All @@ -2249,7 +2248,7 @@ def insert(
indices relative to the end of the circuit instead of the start.
Instructions before the index are not shifted. Instructions that
were at or after the index are shifted forwards.
were at or after the index are shifted forwards as needed.
operation: The object to insert. This can be a single
stim.CircuitInstruction or an entire stim.Circuit.
Expand All @@ -2273,8 +2272,7 @@ def insert(
stim.Circuit('''
H 0
Y 3 4 5
S 1
S 999
S 1 999
CX 0 1
CZ 2 3
X 2
Expand Down
10 changes: 4 additions & 6 deletions doc/stim.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1607,9 +1607,8 @@ class Circuit:
) -> None:
"""Inserts an operation at the given index, pushing existing operations forward.
Note that, unlike when appending operations or parsing stim circuit files,
inserted operations aren't automatically fused into the preceding operation.
This is to avoid creating complicated situations where it's difficult to reason
Beware that inserted operations are automatically fused with the preceding
and following operations, if possible. This can make it complex to reason
about how the indices of operations change in response to insertions.
Args:
Expand All @@ -1620,7 +1619,7 @@ class Circuit:
indices relative to the end of the circuit instead of the start.
Instructions before the index are not shifted. Instructions that
were at or after the index are shifted forwards.
were at or after the index are shifted forwards as needed.
operation: The object to insert. This can be a single
stim.CircuitInstruction or an entire stim.Circuit.
Expand All @@ -1644,8 +1643,7 @@ class Circuit:
stim.Circuit('''
H 0
Y 3 4 5
S 1
S 999
S 1 999
CX 0 1
CZ 2 3
X 2
Expand Down
10 changes: 4 additions & 6 deletions glue/python/src/stim/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1607,9 +1607,8 @@ class Circuit:
) -> None:
"""Inserts an operation at the given index, pushing existing operations forward.
Note that, unlike when appending operations or parsing stim circuit files,
inserted operations aren't automatically fused into the preceding operation.
This is to avoid creating complicated situations where it's difficult to reason
Beware that inserted operations are automatically fused with the preceding
and following operations, if possible. This can make it complex to reason
about how the indices of operations change in response to insertions.
Args:
Expand All @@ -1620,7 +1619,7 @@ class Circuit:
indices relative to the end of the circuit instead of the start.
Instructions before the index are not shifted. Instructions that
were at or after the index are shifted forwards.
were at or after the index are shifted forwards as needed.
operation: The object to insert. This can be a single
stim.CircuitInstruction or an entire stim.Circuit.
Expand All @@ -1644,8 +1643,7 @@ class Circuit:
stim.Circuit('''
H 0
Y 3 4 5
S 1
S 999
S 1 999
CX 0 1
CZ 2 3
X 2
Expand Down
32 changes: 27 additions & 5 deletions src/stim/circuit/circuit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,21 @@ void circuit_read_single_operation(Circuit &circuit, char lead_char, SOURCE read
}

void Circuit::try_fuse_last_two_ops() {
auto &ops = operations;
size_t n = ops.size();
if (n > 1 && ops[n - 2].can_fuse(ops[n - 1])) {
fuse_data(ops[n - 2].targets, ops[n - 1].targets, target_buf);
ops.pop_back();
if (operations.size() >= 2) {
try_fuse_after(operations.size() - 2);
}
}

void Circuit::try_fuse_after(size_t index) {
if (index + 1 >= operations.size()) {
return;
}
if (operations[index].can_fuse(operations[index + 1])) {
fuse_data(operations[index].targets, operations[index + 1].targets, target_buf);
operations.erase(operations.begin() + index + 1);
}
}

template <typename SOURCE>
void circuit_read_operations(Circuit &circuit, SOURCE read_char, READ_CONDITION read_condition) {
auto &ops = circuit.operations;
Expand Down Expand Up @@ -358,6 +366,12 @@ void Circuit::safe_insert(size_t index, const CircuitInstruction &instruction) {
copy.args = arg_buf.take_copy(copy.args);
copy.targets = target_buf.take_copy(copy.targets);
operations.insert(operations.begin() + index, copy);

// Fuse at boundaries.
try_fuse_after(index);
if (index > 0) {
try_fuse_after(index - 1);
}
}

void Circuit::safe_insert(size_t index, const Circuit &circuit) {
Expand All @@ -381,6 +395,14 @@ void Circuit::safe_insert(size_t index, const Circuit &circuit) {
operations[k].args = arg_buf.take_copy(operations[k].args);
}
}

// Fuse at boundaries.
if (!circuit.operations.empty()) {
try_fuse_after(index + circuit.operations.size() - 1);
if (index > 0) {
try_fuse_after(index - 1);
}
}
}

void Circuit::safe_insert_repeat_block(size_t index, uint64_t repeat_count, const Circuit &block) {
Expand Down
1 change: 1 addition & 0 deletions src/stim/circuit/circuit.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ struct Circuit {
std::string describe_instruction_location(size_t instruction_offset) const;

void try_fuse_last_two_ops();
void try_fuse_after(size_t index);
};

void vec_pad_add_mul(std::vector<double> &target, SpanRef<const double> offset, uint64_t mul = 1);
Expand Down
10 changes: 4 additions & 6 deletions src/stim/circuit/circuit.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1134,9 +1134,8 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
Inserts an operation at the given index, pushing existing operations forward.
@signature def insert(self, index: int, operation: Union[stim.CircuitInstruction, stim.Circuit]) -> None:
Note that, unlike when appending operations or parsing stim circuit files,
inserted operations aren't automatically fused into the preceding operation.
This is to avoid creating complicated situations where it's difficult to reason
Beware that inserted operations are automatically fused with the preceding
and following operations, if possible. This can make it complex to reason
about how the indices of operations change in response to insertions.
Args:
Expand All @@ -1147,7 +1146,7 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
indices relative to the end of the circuit instead of the start.
Instructions before the index are not shifted. Instructions that
were at or after the index are shifted forwards.
were at or after the index are shifted forwards as needed.
operation: The object to insert. This can be a single
stim.CircuitInstruction or an entire stim.Circuit.
Expand All @@ -1171,8 +1170,7 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
stim.Circuit('''
H 0
Y 3 4 5
S 1
S 999
S 1 999
CX 0 1
CZ 2 3
X 2
Expand Down
139 changes: 139 additions & 0 deletions src/stim/circuit/circuit.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1769,3 +1769,142 @@ TEST(circuit, generate_test_circuit_with_all_operations) {
}
ASSERT_EQ(seen.size(), NUM_DEFINED_GATES);
}

TEST(circuit, insert_circuit) {
Circuit c(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT");
c.safe_insert(2, Circuit(R"CIRCUIT(
H 1
X 3
S 2
)CIRCUIT"));
ASSERT_EQ(c.operations.size(), 5);
ASSERT_EQ(c, Circuit(R"CIRCUIT(
CX 0 1
H 0 1
X 3
S 2 0
CX 0 1
)CIRCUIT"));

c = Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT");
c.safe_insert(0, Circuit(R"CIRCUIT(
H 1
X 3
S 2
)CIRCUIT"));
ASSERT_EQ(c, Circuit(R"CIRCUIT(
H 1
X 3
S 2
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT"));

c = Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT");
c.safe_insert(4, Circuit(R"CIRCUIT(
H 1
X 3
S 2
)CIRCUIT"));
ASSERT_EQ(c, Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
H 1
X 3
S 2
)CIRCUIT"));
}

TEST(circuit, insert_instruction) {
Circuit c = Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT");
c.safe_insert(2, Circuit("H 1").operations[0]);
ASSERT_EQ(c, Circuit(R"CIRCUIT(
CX 0 1
H 0 1
S 0
CX 0 1
)CIRCUIT"));

c = Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT");
c.safe_insert(2, Circuit("S 1").operations[0]);
ASSERT_EQ(c, Circuit(R"CIRCUIT(
CX 0 1
H 0
S 1 0
CX 0 1
)CIRCUIT"));

c = Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT");
c.safe_insert(2, Circuit("X 1").operations[0]);
ASSERT_EQ(c, Circuit(R"CIRCUIT(
CX 0 1
H 0
X 1
S 0
CX 0 1
)CIRCUIT"));

c = Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT");
c.safe_insert(0, Circuit("X 1").operations[0]);
ASSERT_EQ(c, Circuit(R"CIRCUIT(
X 1
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT"));

c = Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
)CIRCUIT");
c.safe_insert(4, Circuit("X 1").operations[0]);
ASSERT_EQ(c, Circuit(R"CIRCUIT(
CX 0 1
H 0
S 0
CX 0 1
X 1
)CIRCUIT"));
}
27 changes: 27 additions & 0 deletions src/stim/stabilizers/pauli_string_pybind_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -928,3 +928,30 @@ def test_pauli_indices():
assert stim.PauliString("_XYZ").pauli_indices("_") == [0]
with pytest.raises(ValueError, match="Invalid character"):
assert stim.PauliString("_XYZ").pauli_indices("k")


def test_before_reset():
assert stim.PauliString("Z").before(stim.Circuit("R 0")) == stim.PauliString("_")
assert stim.PauliString("Z").before(stim.Circuit("MR 0")) == stim.PauliString("_")
assert stim.PauliString("Z").before(stim.Circuit("M 0")) == stim.PauliString("Z")

assert stim.PauliString("X").before(stim.Circuit("RX 0")) == stim.PauliString("_")
assert stim.PauliString("X").before(stim.Circuit("MRX 0")) == stim.PauliString("_")
assert stim.PauliString("X").before(stim.Circuit("MX 0")) == stim.PauliString("X")

assert stim.PauliString("Y").before(stim.Circuit("RY 0")) == stim.PauliString("_")
assert stim.PauliString("Y").before(stim.Circuit("MRY 0")) == stim.PauliString("_")
assert stim.PauliString("Y").before(stim.Circuit("MY 0")) == stim.PauliString("Y")

with pytest.raises(ValueError):
stim.PauliString("Z").before(stim.Circuit("RX 0"))
with pytest.raises(ValueError):
stim.PauliString("Z").before(stim.Circuit("RY 0"))
with pytest.raises(ValueError):
stim.PauliString("Z").before(stim.Circuit("MRX 0"))
with pytest.raises(ValueError):
stim.PauliString("Z").before(stim.Circuit("MRY 0"))
with pytest.raises(ValueError):
stim.PauliString("Z").before(stim.Circuit("MX 0"))
with pytest.raises(ValueError):
stim.PauliString("Z").before(stim.Circuit("MY 0"))
1 change: 1 addition & 0 deletions src/stim/stabilizers/pauli_string_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ struct PauliStringRef {
void check_avoids_MPP(const CircuitInstruction &inst);
void check_avoids_reset(const CircuitInstruction &inst);
void check_avoids_measurement(const CircuitInstruction &inst);
void undo_reset_xyz(const CircuitInstruction &inst);

void do_single_cx(const CircuitInstruction &inst, uint32_t c, uint32_t t);
void do_single_cy(const CircuitInstruction &inst, uint32_t c, uint32_t t);
Expand Down
Loading

0 comments on commit e0d99eb

Please sign in to comment.