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

Add designated initializers to all gate data #660

Merged
merged 1 commit into from
Nov 19, 2023
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
26 changes: 0 additions & 26 deletions src/stim/circuit/gate_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,32 +149,6 @@ const Gate &Gate::inverse() const {
throw std::out_of_range(inv_name + " has no inverse.");
}

Gate::Gate() : name(nullptr) {
}

Gate::Gate(
const char *name,
GateType gate_id,
GateType best_inverse_gate,
uint8_t arg_count,
GateFlags flags,
const char *category,
const char *help,
FixedCapVector<FixedCapVector<std::complex<float>, 4>, 4> unitary_data,
FixedCapVector<const char *, 10> flow_data,
const char *h_s_cx_m_r_decomposition)
: name(name),
flags(flags),
id(gate_id),
arg_count(arg_count),
best_candidate_inverse_id(best_inverse_gate),
category(category),
help(help),
unitary_data(unitary_data),
flow_data(flow_data),
h_s_cx_m_r_decomposition(h_s_cx_m_r_decomposition) {
}

void GateDataMap::add_gate(bool &failed, const Gate &gate) {
assert((size_t)gate.id < NUM_DEFINED_GATES);
const char *c = gate.name;
Expand Down
26 changes: 6 additions & 20 deletions src/stim/circuit/gate_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,19 @@ enum GateFlags : uint16_t {
};

struct Gate {
/// === core gate data used in main tasks such as sampling ===
/// The canonical name of the gate, used when printing it to a circuit file.
const char *name;
/// Bit-packed data describing details of the gate.
GateFlags flags;
const char *name = nullptr;
/// The gate's type, such as stim::GateType::X or stim::GateType::MRZ.
GateType id;
/// The id of the gate inverse to this one, or at least the closest thing to an inverse.
/// Set to GateType::NOT_A_GATE for gates with no inverse.
GateType best_candidate_inverse_id;
/// The number of parens arguments the gate expects (e.g. X_ERROR takes 1, PAULI_CHANNEL_1 takes 3).
/// Set to stim::ARG_COUNT_SYGIL_ANY to indicate any number is allowed (e.g. DETECTOR coordinate data).
uint8_t arg_count;
/// Bit-packed data describing details of the gate.
GateFlags flags;

/// === extended gate data used in secondary tasks such as exporting to other formats ===
/// The id of the gate inverse to this one, or at least the closest thing to an inverse.
/// Set to GateType::NOT_A_GATE for gates with no inverse.
GateType best_candidate_inverse_id;
/// A word describing what sort of gate this is.
const char *category;
/// Prose summary of what the gate is, how it fits into Stim, and how to use it.
Expand All @@ -243,18 +241,6 @@ struct Gate {
inline bool operator!=(const Gate &other) const {
return id != other.id;
}
Gate();
Gate(
const char *name,
GateType gate_id,
GateType best_inverse_gate,
uint8_t arg_count,
GateFlags flags,
const char *category,
const char *help,
FixedCapVector<FixedCapVector<std::complex<float>, 4>, 4> unitary_data,
FixedCapVector<const char *, 10> flow_data,
const char *h_s_cx_m_r_decomposition);

const Gate &inverse() const;

Expand Down
123 changes: 62 additions & 61 deletions src/stim/circuit/gate_data_annotations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ void GateDataMap::add_gate_data_annotations(bool &failed) {
add_gate(
failed,
Gate{
"DETECTOR",
GateType::DETECTOR,
GateType::DETECTOR,
ARG_COUNT_SYGIL_ANY,
(GateFlags)(GATE_ONLY_TARGETS_MEASUREMENT_RECORD | GATE_IS_NOT_FUSABLE | GATE_HAS_NO_EFFECT_ON_QUBITS),
"Z_Annotations",
R"MARKDOWN(
.name = "DETECTOR",
.id = GateType::DETECTOR,
.best_candidate_inverse_id = GateType::DETECTOR,
.arg_count = ARG_COUNT_SYGIL_ANY,
.flags =
(GateFlags)(GATE_ONLY_TARGETS_MEASUREMENT_RECORD | GATE_IS_NOT_FUSABLE | GATE_HAS_NO_EFFECT_ON_QUBITS),
.category = "Z_Annotations",
.help = R"MARKDOWN(
Annotates that a set of measurements can be used to detect errors, because the set's parity should be deterministic.

Note that it is not necessary to say whether the measurement set's parity is even or odd; all that matters is that the
Expand Down Expand Up @@ -96,22 +97,22 @@ Parens Arguments:
DETECTOR(0, 3) rec[-1]
}
)MARKDOWN",
{},
{},
nullptr,
.unitary_data = {},
.flow_data = {},
.h_s_cx_m_r_decomposition = nullptr,
});

add_gate(
failed,
Gate{
"OBSERVABLE_INCLUDE",
GateType::OBSERVABLE_INCLUDE,
GateType::OBSERVABLE_INCLUDE,
1,
(GateFlags)(GATE_ONLY_TARGETS_MEASUREMENT_RECORD | GATE_IS_NOT_FUSABLE | GATE_ARGS_ARE_UNSIGNED_INTEGERS |
GATE_HAS_NO_EFFECT_ON_QUBITS),
"Z_Annotations",
R"MARKDOWN(
.name = "OBSERVABLE_INCLUDE",
.id = GateType::OBSERVABLE_INCLUDE,
.best_candidate_inverse_id = GateType::OBSERVABLE_INCLUDE,
.arg_count = 1,
.flags = (GateFlags)(GATE_ONLY_TARGETS_MEASUREMENT_RECORD | GATE_IS_NOT_FUSABLE |
GATE_ARGS_ARE_UNSIGNED_INTEGERS | GATE_HAS_NO_EFFECT_ON_QUBITS),
.category = "Z_Annotations",
.help = R"MARKDOWN(
Adds measurement records to a specified logical observable.

A potential point of confusion here is that Stim's notion of a logical observable is nothing more than a set of
Expand Down Expand Up @@ -167,21 +168,21 @@ Parens Arguments:
# ...and the one before that.
OBSERVABLE_INCLUDE(1) rec[-2]
)MARKDOWN",
{},
{},
nullptr,
.unitary_data = {},
.flow_data = {},
.h_s_cx_m_r_decomposition = nullptr,
});

add_gate(
failed,
Gate{
"TICK",
GateType::TICK,
GateType::TICK,
0,
(GateFlags)(GATE_IS_NOT_FUSABLE | GATE_TAKES_NO_TARGETS | GATE_HAS_NO_EFFECT_ON_QUBITS),
"Z_Annotations",
R"MARKDOWN(
.name = "TICK",
.id = GateType::TICK,
.best_candidate_inverse_id = GateType::TICK,
.arg_count = 0,
.flags = (GateFlags)(GATE_IS_NOT_FUSABLE | GATE_TAKES_NO_TARGETS | GATE_HAS_NO_EFFECT_ON_QUBITS),
.category = "Z_Annotations",
.help = R"MARKDOWN(
Annotates the end of a layer of gates, or that time is advancing.

This instruction is not necessary, it has no effect on simulations, but it can be used by tools that are transforming or
Expand Down Expand Up @@ -213,21 +214,21 @@ Parens Arguments:
# Empty time step.
TICK
)MARKDOWN",
{},
{},
nullptr,
.unitary_data = {},
.flow_data = {},
.h_s_cx_m_r_decomposition = nullptr,
});

add_gate(
failed,
Gate{
"QUBIT_COORDS",
GateType::QUBIT_COORDS,
GateType::QUBIT_COORDS,
ARG_COUNT_SYGIL_ANY,
(GateFlags)(GATE_IS_NOT_FUSABLE | GATE_HAS_NO_EFFECT_ON_QUBITS),
"Z_Annotations",
R"MARKDOWN(
.name = "QUBIT_COORDS",
.id = GateType::QUBIT_COORDS,
.best_candidate_inverse_id = GateType::QUBIT_COORDS,
.arg_count = ARG_COUNT_SYGIL_ANY,
.flags = (GateFlags)(GATE_IS_NOT_FUSABLE | GATE_HAS_NO_EFFECT_ON_QUBITS),
.category = "Z_Annotations",
.help = R"MARKDOWN(
Annotates the location of a qubit.

Coordinates are not required and have no effect on simulations, but can be useful to tools consuming the circuit. For
Expand Down Expand Up @@ -258,21 +259,21 @@ Parens Arguments:
QUBIT_COORDS(1, 0) 2
QUBIT_COORDS(1, 1) 3
)MARKDOWN",
{},
{},
nullptr,
.unitary_data = {},
.flow_data = {},
.h_s_cx_m_r_decomposition = nullptr,
});

add_gate(
failed,
Gate{
"SHIFT_COORDS",
GateType::SHIFT_COORDS,
GateType::SHIFT_COORDS,
ARG_COUNT_SYGIL_ANY,
(GateFlags)(GATE_IS_NOT_FUSABLE | GATE_TAKES_NO_TARGETS | GATE_HAS_NO_EFFECT_ON_QUBITS),
"Z_Annotations",
R"MARKDOWN(
.name = "SHIFT_COORDS",
.id = GateType::SHIFT_COORDS,
.best_candidate_inverse_id = GateType::SHIFT_COORDS,
.arg_count = ARG_COUNT_SYGIL_ANY,
.flags = (GateFlags)(GATE_IS_NOT_FUSABLE | GATE_TAKES_NO_TARGETS | GATE_HAS_NO_EFFECT_ON_QUBITS),
.category = "Z_Annotations",
.help = R"MARKDOWN(
Accumulates offsets that affect qubit coordinates and detector coordinates.

Note: when qubit/detector coordinates use fewer dimensions than SHIFT_COORDS, the offsets from the additional dimensions
Expand Down Expand Up @@ -307,21 +308,21 @@ Parens Arguments:
SHIFT_COORDS(0, 1) # Advance 2nd coordinate to track loop iterations.
}
)MARKDOWN",
{},
{},
nullptr,
.unitary_data = {},
.flow_data = {},
.h_s_cx_m_r_decomposition = nullptr,
});

add_gate(
failed,
Gate{
"MPAD",
GateType::MPAD,
GateType::MPAD,
0,
GATE_PRODUCES_RESULTS,
"Z_Annotations",
R"MARKDOWN(
.name = "MPAD",
.id = GateType::MPAD,
.best_candidate_inverse_id = GateType::MPAD,
.arg_count = 0,
.flags = GATE_PRODUCES_RESULTS,
.category = "Z_Annotations",
.help = R"MARKDOWN(
Pads the measurement record with the listed measurement results.

This can be useful for ensuring measurements are aligned to word boundaries, or that the
Expand All @@ -344,8 +345,8 @@ of measured qubits varies.
# Append a series of results to the measurement record.
MPAD 0 0 1 0 1
)MARKDOWN",
{},
{},
nullptr,
.unitary_data = {},
.flow_data = {},
.h_s_cx_m_r_decomposition = nullptr,
});
}
20 changes: 10 additions & 10 deletions src/stim/circuit/gate_data_blocks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ void GateDataMap::add_gate_data_blocks(bool &failed) {
add_gate(
failed,
Gate{
"REPEAT",
GateType::REPEAT,
GateType::REPEAT,
0,
(GateFlags)(GATE_IS_BLOCK | GATE_IS_NOT_FUSABLE),
"Y_Control Flow",
R"MARKDOWN(
.name = "REPEAT",
.id = GateType::REPEAT,
.best_candidate_inverse_id = GateType::REPEAT,
.arg_count = 0,
.flags = (GateFlags)(GATE_IS_BLOCK | GATE_IS_NOT_FUSABLE),
.category = "Y_Control Flow",
.help = R"MARKDOWN(
Repeats the instructions in its body N times.

Currently, repetition counts of 0 are not allowed because they create corner cases with ambiguous resolutions.
Expand Down Expand Up @@ -55,8 +55,8 @@ Parens Arguments:
DETECTOR rec[-1] rec[-3]
}
)MARKDOWN",
{},
{},
nullptr,
.unitary_data = {},
.flow_data = {},
.h_s_cx_m_r_decomposition = nullptr,
});
}
Loading
Loading