Skip to content

Commit

Permalink
Gateset: fix invalid repr when gateset is empty (quantumlib#5322)
Browse files Browse the repository at this point in the history
  • Loading branch information
verult authored May 2, 2022
1 parent 1bfe848 commit 5132322
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cirq/ops/gateset.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,9 +525,10 @@ def _value_equality_values_(self) -> Any:

def __repr__(self) -> str:
name_str = f'name = "{self.name}", ' if self.name is not None else ''
gates_str = f'{self._gates_repr_str}, ' if len(self._gates_repr_str) > 0 else ''
return (
f'cirq.Gateset('
f'{self._gates_repr_str}, '
f'{gates_str}'
f'{name_str}'
f'unroll_circuit_op = {self._unroll_circuit_op})'
)
Expand Down
11 changes: 6 additions & 5 deletions cirq/ops/gateset_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,12 @@ def test_gateset_init():
)


def test_gateset_repr_and_str():
cirq.testing.assert_equivalent_repr(gateset)
assert gateset.name in str(gateset)
for gate_family in gateset.gates:
assert str(gate_family) in str(gateset)
@pytest.mark.parametrize('g', [gateset, cirq.Gateset(name='empty gateset')])
def test_gateset_repr_and_str(g):
cirq.testing.assert_equivalent_repr(g)
assert g.name in str(g)
for gate_family in g.gates:
assert str(gate_family) in str(g)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 5132322

Please sign in to comment.