Skip to content

Commit e6fd563

Browse files
authored
Update circuit data references plus some house cleaning (#850)
- Rerun the getting started notebook with v1.14 - Run autoformatter over the code
1 parent 2581b90 commit e6fd563

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2541
-2219
lines changed

doc/circuit_data_references.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@
2929
- [arXiv:2407.13826 ](https://arxiv.org/abs/2407.13826)[Stim circuits for 'Designing fault-tolerant circuits using detector error models'](https://github.com/peter-janderks/short_measurement_schedules_simulations/tree/main/stim_circuits)
3030
- [arXiv:2408.00758](https://arxiv.org/abs/2408.00758)[Stim circuits for ``To reset, or not to reset -- that is the question" manuscript](https://zenodo.org/records/13152440)
3131
- [arXiv:2408.11894](https://arxiv.org/abs/2408.11894)[Stim circuits for 'Automated Synthesis of Fault-Tolerant State Preparation Circuits for Quantum Error Correction Codes'](https://github.com/cda-tum/mqt-qecc/tree/main/src/mqt/qecc/ft_stateprep/eval/circuits)
32+
- [arXiv:2408.13687](https://arxiv.org/abs/2408.13687)[Data for "Quantum error correction below the surface code threshold"](https://zenodo.org/records/13273331)
33+
- [arXiv:2409.17595](https://arxiv.org/abs/2409.17595)[Data for "Magic state cultivation: growing T states as cheap as CNOT gates"](https://zenodo.org/records/13777072)

doc/getting_started.ipynb

Lines changed: 851 additions & 761 deletions
Large diffs are not rendered by default.

src/stim/circuit/circuit.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ void circuit_read_single_operation(Circuit &circuit, char lead_char, SOURCE read
230230
}
231231

232232
circuit.tag_buf.commit_tail();
233-
circuit.operations.push_back(CircuitInstruction(gate.id, circuit.arg_buf.commit_tail(), circuit.target_buf.commit_tail(), tail_tag));
233+
circuit.operations.push_back(
234+
CircuitInstruction(gate.id, circuit.arg_buf.commit_tail(), circuit.target_buf.commit_tail(), tail_tag));
234235
}
235236

236237
void Circuit::try_fuse_last_two_ops() {
@@ -411,7 +412,8 @@ void Circuit::safe_insert(size_t index, const Circuit &circuit) {
411412
}
412413
}
413414

414-
void Circuit::safe_insert_repeat_block(size_t index, uint64_t repeat_count, const Circuit &block, std::string_view tag) {
415+
void Circuit::safe_insert_repeat_block(
416+
size_t index, uint64_t repeat_count, const Circuit &block, std::string_view tag) {
415417
if (repeat_count == 0) {
416418
throw std::invalid_argument("Can't repeat 0 times.");
417419
}
@@ -426,8 +428,7 @@ void Circuit::safe_insert_repeat_block(size_t index, uint64_t repeat_count, cons
426428
operations.insert(operations.begin() + index, CircuitInstruction(GateType::REPEAT, {}, targets, tag));
427429
}
428430

429-
void Circuit::safe_append_reversed_targets(
430-
CircuitInstruction instruction, bool reverse_in_pairs) {
431+
void Circuit::safe_append_reversed_targets(CircuitInstruction instruction, bool reverse_in_pairs) {
431432
if (reverse_in_pairs) {
432433
if (instruction.targets.size() % 2 != 0) {
433434
throw std::invalid_argument("targets.size() % 2 != 0");
@@ -779,7 +780,8 @@ const Circuit Circuit::aliased_noiseless_circuit() const {
779780
auto &tail = result.target_buf.tail;
780781
tail.ptr_end = tail.ptr_start + op.targets.size();
781782
memset(tail.ptr_start, 0, (tail.ptr_end - tail.ptr_start) * sizeof(GateTarget));
782-
result.operations.push_back(CircuitInstruction(GateType::MPAD, {}, result.target_buf.commit_tail(), op.tag));
783+
result.operations.push_back(
784+
CircuitInstruction(GateType::MPAD, {}, result.target_buf.commit_tail(), op.tag));
783785
result.try_fuse_last_two_ops();
784786
} else {
785787
// Drop result flip probability.
@@ -812,7 +814,8 @@ Circuit Circuit::without_noise() const {
812814
tail.ptr_end = tail.ptr_start + op.targets.size();
813815
memset(tail.ptr_start, 0, (tail.ptr_end - tail.ptr_start) * sizeof(GateTarget));
814816
auto tag = result.tag_buf.take_copy(op.tag);
815-
result.operations.push_back(CircuitInstruction(GateType::MPAD, {}, result.target_buf.commit_tail(), tag));
817+
result.operations.push_back(
818+
CircuitInstruction(GateType::MPAD, {}, result.target_buf.commit_tail(), tag));
816819
} else {
817820
// Drop result flip probabilities.
818821
auto targets = result.target_buf.take_copy(op.targets);

src/stim/circuit/circuit.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ struct Circuit {
122122
void safe_insert(size_t index, const Circuit &circuit);
123123

124124
/// Appends the given gate, but with targets reversed.
125-
void safe_append_reversed_targets(
126-
CircuitInstruction instruction, bool reverse_in_pairs);
125+
void safe_append_reversed_targets(CircuitInstruction instruction, bool reverse_in_pairs);
127126

128127
/// Resets the circuit back to an empty circuit.
129128
void clear();

src/stim/circuit/circuit.pybind.cc

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ pybind11::object circuit_get_item(const Circuit &self, const pybind11::object &i
187187

188188
auto &op = self.operations[index];
189189
if (op.gate_type == GateType::REPEAT) {
190-
return pybind11::cast(CircuitRepeatBlock{op.repeat_block_rep_count(), op.repeat_block_body(self), pybind11::str(op.tag)});
190+
return pybind11::cast(
191+
CircuitRepeatBlock{op.repeat_block_rep_count(), op.repeat_block_body(self), pybind11::str(op.tag)});
191192
}
192193
std::vector<GateTarget> targets;
193194
for (const auto &e : op.targets) {
@@ -292,12 +293,13 @@ void circuit_append(
292293
}
293294

294295
const PyCircuitInstruction &instruction = pybind11::cast<PyCircuitInstruction>(obj);
295-
self.safe_append(CircuitInstruction{
296-
instruction.gate_type,
297-
instruction.gate_args,
298-
instruction.targets,
299-
pybind11::cast<std::string_view>(instruction.tag),
300-
});
296+
self.safe_append(
297+
CircuitInstruction{
298+
instruction.gate_type,
299+
instruction.gate_args,
300+
instruction.targets,
301+
pybind11::cast<std::string_view>(instruction.tag),
302+
});
301303
} else if (pybind11::isinstance<CircuitRepeatBlock>(obj)) {
302304
if (!raw_targets.empty() || !arg.is_none()) {
303305
throw std::invalid_argument("Can't specify `targets` or `arg` when appending a stim.CircuitRepeatBlock.");
@@ -2040,13 +2042,14 @@ void stim_pybind::pybind_circuit_methods(pybind11::module &, pybind11::class_<Ci
20402042
)DOC")
20412043
.data());
20422044

2043-
c.def(pybind11::pickle(
2044-
[](const Circuit &self) -> pybind11::str {
2045-
return self.str();
2046-
},
2047-
[](const pybind11::str &text) {
2048-
return Circuit(pybind11::cast<std::string_view>(text));
2049-
}));
2045+
c.def(
2046+
pybind11::pickle(
2047+
[](const Circuit &self) -> pybind11::str {
2048+
return self.str();
2049+
},
2050+
[](const pybind11::str &text) {
2051+
return Circuit(pybind11::cast<std::string_view>(text));
2052+
}));
20502053

20512054
c.def(
20522055
"shortest_graphlike_error",

src/stim/circuit/circuit_instruction.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ bool CircuitInstruction::operator==(const CircuitInstruction &other) const {
302302
return gate_type == other.gate_type && args == other.args && targets == other.targets && tag == other.tag;
303303
}
304304
bool CircuitInstruction::approx_equals(const CircuitInstruction &other, double atol) const {
305-
if (gate_type != other.gate_type || targets != other.targets || args.size() != other.args.size() || tag != other.tag) {
305+
if (gate_type != other.gate_type || targets != other.targets || args.size() != other.args.size() ||
306+
tag != other.tag) {
306307
return false;
307308
}
308309
for (size_t k = 0; k < args.size(); k++) {

src/stim/circuit/circuit_instruction.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ struct CircuitInstruction {
7676
std::string_view tag;
7777

7878
CircuitInstruction() = delete;
79-
CircuitInstruction(GateType gate_type, SpanRef<const double> args, SpanRef<const GateTarget> targets, std::string_view tag);
79+
CircuitInstruction(
80+
GateType gate_type, SpanRef<const double> args, SpanRef<const GateTarget> targets, std::string_view tag);
8081

8182
/// Computes number of qubits, number of measurements, etc.
8283
CircuitStats compute_stats(const Circuit *host) const;
@@ -129,7 +130,9 @@ struct CircuitInstruction {
129130
end = start + 1;
130131
} else if (flags & stim::GateFlags::GATE_TARGETS_PAIRS) {
131132
end = start + 2;
132-
} else if ((flags & stim::GateFlags::GATE_TARGETS_PAULI_STRING) && !(flags & stim::GateFlags::GATE_TARGETS_COMBINERS)) {
133+
} else if (
134+
(flags & stim::GateFlags::GATE_TARGETS_PAULI_STRING) &&
135+
!(flags & stim::GateFlags::GATE_TARGETS_COMBINERS)) {
133136
// like CORRELATED_ERROR
134137
end = targets.size();
135138
} else if (flags & stim::GateFlags::GATE_ONLY_TARGETS_MEASUREMENT_RECORD) {

src/stim/circuit/circuit_instruction.pybind.cc

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ using namespace stim;
99
using namespace stim_pybind;
1010

1111
PyCircuitInstruction::PyCircuitInstruction(
12-
std::string_view name, std::span<pybind11::object> init_targets, std::span<double> init_gate_args, pybind11::str tag)
12+
std::string_view name,
13+
std::span<pybind11::object> init_targets,
14+
std::span<double> init_gate_args,
15+
pybind11::str tag)
1316
: gate_type(GATE_DATA.at(name).id), tag(tag) {
1417
for (const auto &obj : init_gate_args) {
1518
gate_args.push_back(obj);
@@ -49,7 +52,8 @@ PyCircuitInstruction PyCircuitInstruction::from_instruction(CircuitInstruction i
4952
}
5053

5154
bool PyCircuitInstruction::operator==(const PyCircuitInstruction &other) const {
52-
return gate_type == other.gate_type && targets == other.targets && gate_args == other.gate_args && pybind11::cast<std::string_view>(tag) == pybind11::cast<std::string_view>(other.tag);
55+
return gate_type == other.gate_type && targets == other.targets && gate_args == other.gate_args &&
56+
pybind11::cast<std::string_view>(tag) == pybind11::cast<std::string_view>(other.tag);
5357
}
5458
bool PyCircuitInstruction::operator!=(const PyCircuitInstruction &other) const {
5559
return !(*this == other);
@@ -83,12 +87,7 @@ std::string PyCircuitInstruction::str() const {
8387
}
8488

8589
CircuitInstruction PyCircuitInstruction::as_operation_ref() const {
86-
return CircuitInstruction{
87-
gate_type,
88-
gate_args,
89-
targets,
90-
pybind11::cast<std::string_view>(tag)
91-
};
90+
return CircuitInstruction{gate_type, gate_args, targets, pybind11::cast<std::string_view>(tag)};
9291
}
9392
PyCircuitInstruction::operator CircuitInstruction() const {
9493
return as_operation_ref();
@@ -149,20 +148,22 @@ pybind11::class_<PyCircuitInstruction> stim_pybind::pybind_circuit_instruction(p
149148
}
150149
void stim_pybind::pybind_circuit_instruction_methods(pybind11::module &m, pybind11::class_<PyCircuitInstruction> &c) {
151150
c.def(
152-
pybind11::init([](std::string_view name, pybind11::object targets, pybind11::object gate_args, pybind11::str tag) -> PyCircuitInstruction {
153-
if (targets.is_none() and gate_args.is_none() && !pybind11::cast<bool>(pybind11::bool_(tag))) {
154-
return PyCircuitInstruction::from_str(name);
155-
}
156-
std::vector<double> conv_args;
157-
std::vector<pybind11::object> conv_targets;
158-
if (!gate_args.is_none()) {
159-
conv_args = pybind11::cast<std::vector<double>>(gate_args);
160-
}
161-
if (!targets.is_none()) {
162-
conv_targets = pybind11::cast<std::vector<pybind11::object>>(targets);
163-
}
164-
return PyCircuitInstruction(name, conv_targets, conv_args, tag);
165-
}),
151+
pybind11::init(
152+
[](std::string_view name, pybind11::object targets, pybind11::object gate_args, pybind11::str tag)
153+
-> PyCircuitInstruction {
154+
if (targets.is_none() and gate_args.is_none() && !pybind11::cast<bool>(pybind11::bool_(tag))) {
155+
return PyCircuitInstruction::from_str(name);
156+
}
157+
std::vector<double> conv_args;
158+
std::vector<pybind11::object> conv_targets;
159+
if (!gate_args.is_none()) {
160+
conv_args = pybind11::cast<std::vector<double>>(gate_args);
161+
}
162+
if (!targets.is_none()) {
163+
conv_targets = pybind11::cast<std::vector<pybind11::object>>(targets);
164+
}
165+
return PyCircuitInstruction(name, conv_targets, conv_args, tag);
166+
}),
166167
pybind11::arg("name"),
167168
pybind11::arg("targets") = pybind11::none(),
168169
pybind11::arg("gate_args") = pybind11::none(),

src/stim/circuit/circuit_instruction.pybind.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ struct PyCircuitInstruction {
3333
PyCircuitInstruction(
3434
std::string_view name, std::span<pybind11::object> targets, std::span<double> gate_args, pybind11::str tag);
3535
PyCircuitInstruction(
36-
stim::GateType gate_type, std::vector<stim::GateTarget> targets, std::vector<double> gate_args, pybind11::str tag);
36+
stim::GateType gate_type,
37+
std::vector<stim::GateTarget> targets,
38+
std::vector<double> gate_args,
39+
pybind11::str tag);
3740
static PyCircuitInstruction from_str(std::string_view text);
3841
static PyCircuitInstruction from_instruction(stim::CircuitInstruction instruction);
3942

src/stim/circuit/circuit_instruction.test.cc

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,36 +31,44 @@ TEST(circuit_instruction, for_combined_targets) {
3131
});
3232
return results;
3333
};
34-
ASSERT_EQ(get_k(0), (std::vector<std::vector<GateTarget>>{
35-
}));
36-
ASSERT_EQ(get_k(1), (std::vector<std::vector<GateTarget>>{
37-
}));
38-
ASSERT_EQ(get_k(2), (std::vector<std::vector<GateTarget>>{
39-
{GateTarget::qubit(1)},
40-
}));
41-
ASSERT_EQ(get_k(3), (std::vector<std::vector<GateTarget>>{
42-
{GateTarget::qubit(0)},
43-
{GateTarget::qubit(2)},
44-
}));
45-
ASSERT_EQ(get_k(4), (std::vector<std::vector<GateTarget>>{
46-
}));
47-
ASSERT_EQ(get_k(5), (std::vector<std::vector<GateTarget>>{
48-
{GateTarget::qubit(0), GateTarget::qubit(1)},
49-
{GateTarget::qubit(2), GateTarget::qubit(3)},
50-
}));
51-
ASSERT_EQ(get_k(6), (std::vector<std::vector<GateTarget>>{
52-
{GateTarget::qubit(3), GateTarget::qubit(5)},
53-
}));
54-
ASSERT_EQ(get_k(7), (std::vector<std::vector<GateTarget>>{
55-
}));
56-
ASSERT_EQ(get_k(8), (std::vector<std::vector<GateTarget>>{
57-
{GateTarget::x(0), GateTarget::combiner(), GateTarget::x(1), GateTarget::combiner(), GateTarget::z(2)},
58-
{GateTarget::z(7)},
59-
{GateTarget::x(5), GateTarget::combiner(), GateTarget::x(9)},
60-
}));
61-
ASSERT_EQ(get_k(9), (std::vector<std::vector<GateTarget>>{
62-
{GateTarget::z(5)},
63-
}));
34+
ASSERT_EQ(get_k(0), (std::vector<std::vector<GateTarget>>{}));
35+
ASSERT_EQ(get_k(1), (std::vector<std::vector<GateTarget>>{}));
36+
ASSERT_EQ(
37+
get_k(2),
38+
(std::vector<std::vector<GateTarget>>{
39+
{GateTarget::qubit(1)},
40+
}));
41+
ASSERT_EQ(
42+
get_k(3),
43+
(std::vector<std::vector<GateTarget>>{
44+
{GateTarget::qubit(0)},
45+
{GateTarget::qubit(2)},
46+
}));
47+
ASSERT_EQ(get_k(4), (std::vector<std::vector<GateTarget>>{}));
48+
ASSERT_EQ(
49+
get_k(5),
50+
(std::vector<std::vector<GateTarget>>{
51+
{GateTarget::qubit(0), GateTarget::qubit(1)},
52+
{GateTarget::qubit(2), GateTarget::qubit(3)},
53+
}));
54+
ASSERT_EQ(
55+
get_k(6),
56+
(std::vector<std::vector<GateTarget>>{
57+
{GateTarget::qubit(3), GateTarget::qubit(5)},
58+
}));
59+
ASSERT_EQ(get_k(7), (std::vector<std::vector<GateTarget>>{}));
60+
ASSERT_EQ(
61+
get_k(8),
62+
(std::vector<std::vector<GateTarget>>{
63+
{GateTarget::x(0), GateTarget::combiner(), GateTarget::x(1), GateTarget::combiner(), GateTarget::z(2)},
64+
{GateTarget::z(7)},
65+
{GateTarget::x(5), GateTarget::combiner(), GateTarget::x(9)},
66+
}));
67+
ASSERT_EQ(
68+
get_k(9),
69+
(std::vector<std::vector<GateTarget>>{
70+
{GateTarget::z(5)},
71+
}));
6472
}
6573

6674
TEST(circuit_instruction, for_combined_targets_works_on_all) {

0 commit comments

Comments
 (0)