Skip to content

Commit

Permalink
Fix more cases of assuming is_unitary == (unitary_matrix is not None)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strilanc committed Oct 28, 2024
1 parent 6ab1f13 commit 528dd99
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/stim/cmd/command_help.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void print_stabilizer_generators(Acc &out, const Gate &gate) {
}

void print_bloch_vector(Acc &out, const Gate &gate) {
if (!(gate.flags & GATE_IS_UNITARY) || (gate.flags & GATE_TARGETS_PAIRS)) {
if (!(gate.flags & GATE_IS_UNITARY) || !(gate.flags & GATE_IS_SINGLE_QUBIT_GATE)) {
return;
}

Expand Down Expand Up @@ -343,7 +343,7 @@ void print_bloch_vector(Acc &out, const Gate &gate) {
}

void print_unitary_matrix(Acc &out, const Gate &gate) {
if (!(gate.flags & GATE_IS_UNITARY)) {
if (!gate.has_known_unitary_matrix()) {
return;
}
auto matrix = gate.unitary();
Expand Down
4 changes: 4 additions & 0 deletions src/stim/gates/gates.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ std::array<float, 4> Gate::to_axis_angle() const {
return {rx, ry, rz, acosf(rs) * 2};
}

bool Gate::has_known_unitary_matrix() const {
return (flags & GateFlags::GATE_IS_UNITARY) && (flags & (GateFlags::GATE_IS_SINGLE_QUBIT_GATE | GateFlags::GATE_TARGETS_PAIRS));
}

std::vector<std::vector<std::complex<float>>> Gate::unitary() const {
if (unitary_data.size() != 2 && unitary_data.size() != 4) {
throw std::out_of_range(std::string(name) + " doesn't have 1q or 2q unitary data.");
Expand Down
8 changes: 7 additions & 1 deletion src/stim/gates/gates.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ struct Gate {

template <size_t W>
std::vector<Flow<W>> flows() const {
if (flags & GateFlags::GATE_IS_UNITARY) {
if (has_known_unitary_matrix()) {
auto t = tableau<W>();
if (flags & GateFlags::GATE_TARGETS_PAIRS) {
return {
Expand All @@ -285,6 +285,12 @@ struct Gate {
bool is_symmetric() const;
GateType hadamard_conjugated(bool ignoring_sign) const;

/// Determines if the gate has a specified unitary matrix.
///
/// Some unitary gates, such as SPP, don't have a specified matrix because the
/// matrix depends crucially on the targets.
bool has_known_unitary_matrix() const;

/// Converts a single qubit unitary gate into an euler-angles rotation.
///
/// Returns:
Expand Down
2 changes: 1 addition & 1 deletion src/stim/gates/gates.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pybind11::object gate_tableau(const Gate &self) {
return pybind11::none();
}
pybind11::object gate_unitary_matrix(const Gate &self) {
if ((self.flags & GateFlags::GATE_IS_UNITARY) && (self.flags & (GateFlags::GATE_IS_SINGLE_QUBIT_GATE | GateFlags::GATE_TARGETS_PAIRS))) {
if (self.has_known_unitary_matrix()) {
auto r = self.unitary();
auto n = r.size();
std::complex<float> *buffer = new std::complex<float>[n * n];
Expand Down
7 changes: 7 additions & 0 deletions src/stim/py/stim_pybind_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ def test_main_write_to_file():
assert "Generated repetition_code" in f.read()


def test_main_help(capsys):
assert stim.main(command_line_args=["help"]) == 0
captured = capsys.readouterr()
assert captured.err == ""
assert 'Available stim commands' in captured.out


def test_main_redirects_stdout(capsys):
assert stim.main(command_line_args=[
"gen",
Expand Down

0 comments on commit 528dd99

Please sign in to comment.