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

Change qubit str representation #5343

Merged
merged 14 commits into from
May 16, 2022
Prev Previous commit
Next Next commit
Switch to q(0) and fix a bunch of tests.
  • Loading branch information
dstrain115 committed May 10, 2022
commit e0e001be6dc87a6f8ca57dd4fd39b8bf42ed01af
4 changes: 2 additions & 2 deletions cirq-core/cirq/circuits/circuit_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ def test_string_format():
== f"""\
[ 0: ───X───X─── ]
[ │ ]
[ 1: ───H───@─── ](qubit_map={{1: 2}}, parent_path=('outer', 'inner'),\
[ 1: ───H───@─── ](qubit_map={{q(1): q(2)}}, parent_path=('outer', 'inner'),\
repetition_ids=['a', 'b', 'c'])"""
)
assert (
Expand Down Expand Up @@ -611,7 +611,7 @@ def test_string_format():
assert (
str(op3)
== f"""\
[ 0: ───X^b───M('m')─── ](qubit_map={{0: 1}}, \
[ 0: ───X^b───M('m')─── ](qubit_map={{q(0): q(1)}}, \
key_map={{m: p}}, params={{b: 2}})"""
)
assert (
Expand Down
2 changes: 1 addition & 1 deletion cirq-core/cirq/circuits/qasm_output_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ def test_reset():
include "qelib1.inc";


// Qubits: [0, 1]
// Qubits: [q(0), q(1)]
qreg q[2];


Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/devices/line_qubit.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def __repr__(self) -> str:
return f"cirq.LineQid({self.x}, dimension={self.dimension})"

def __str__(self) -> str:
return f"q{self.x} (d={self.dimension})"
return f"q({self.x}) (d={self.dimension})"

def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
Expand Down Expand Up @@ -246,7 +246,7 @@ def __repr__(self) -> str:
return f"cirq.LineQubit({self.x})"

def __str__(self) -> str:
return f"q{self.x}"
return f"q({self.x})"

def _circuit_diagram_info_(
self, args: 'cirq.CircuitDiagramInfoArgs'
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/ops/classically_controlled_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def test_qasm():
include "qelib1.inc";


// Qubits: [q0, q1]
// Qubits: [q(0), q(1)]
qreg q[2];
creg m_a[1];

Expand Down Expand Up @@ -416,7 +416,7 @@ def test_decompose():
def test_str():
q0 = cirq.LineQubit(0)
op = cirq.X(q0).with_classical_controls('a')
assert str(op) == 'X(q0).with_classical_controls(a)'
assert str(op) == 'X(q(0)).with_classical_controls(a)'


def test_scope_local():
Expand Down
14 changes: 7 additions & 7 deletions cirq-core/cirq/ops/controlled_gate_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,11 +514,11 @@ def test_circuit_diagram():
cirq.testing.assert_has_diagram(
c,
"""
0: ───@───────
0: ───@─────────
1: ───H(q1)───
1: ───H(q(1))───
2: ───H(q2)───
2: ───H(q(2))───
""",
)

Expand All @@ -530,13 +530,13 @@ def test_circuit_diagram():
cirq.testing.assert_has_diagram(
c,
"""
0 (d=3): ───@─────────────
0 (d=3): ───@───────────────
1 (d=3): ───(0,1)─────────
1 (d=3): ───(0,1)───────────
2 (d=3): ───(0,2)─────────
2 (d=3): ───(0,2)───────────
3 (d=2): ───H(q3 (d=2))───
3 (d=2): ───H(q(3) (d=2))───
""",
)

Expand Down
20 changes: 10 additions & 10 deletions cirq-core/cirq/ops/controlled_operation_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ def test_circuit_diagram():
cirq.testing.assert_has_diagram(
c,
"""
0: ───@───────
0: ───@─────────
1: ───H(q1)───
1: ───H(q(1))───
2: ───H(q2)───
2: ───H(q(2))───
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit odd to me that controlled operation shows the applied qubits like this, since that should be clear from the qubit lines. Would be nice to change this to just show the gate diagram info if possible. But that could be a future PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a custom gate in the test code and is not used outside the test, so I am not too worried about it. Normal controlled operations are unaffected.

""",
)

Expand All @@ -247,11 +247,11 @@ def test_circuit_diagram():
cirq.testing.assert_has_diagram(
c,
"""
0: ───@───────
0: ───@─────────
1: ───@───────
1: ───@─────────
2: ───H(q2)───
2: ───H(q(2))───
""",
)

Expand All @@ -266,13 +266,13 @@ def test_circuit_diagram():
cirq.testing.assert_has_diagram(
c,
"""
0 (d=3): ───@─────────────
0 (d=3): ───@───────────────
1 (d=3): ───(0,1)─────────
1 (d=3): ───(0,1)───────────
2 (d=3): ───(0,2)─────────
2 (d=3): ───(0,2)───────────
3 (d=2): ───H(q3 (d=2))───
3 (d=2): ───H(q(3) (d=2))───
""",
)

Expand Down
12 changes: 6 additions & 6 deletions cirq-core/cirq/ops/linear_combinations_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,16 +984,16 @@ def test_add_number_paulistring():
def test_pauli_sum_formatting():
q = cirq.LineQubit.range(2)
pauli = cirq.X(q[0])
assert str(pauli) == 'X(q0)'
assert str(pauli) == 'X(q(0))'
paulistr = cirq.X(q[0]) * cirq.X(q[1])
assert str(paulistr) == 'X(q0)*X(q1)'
assert str(paulistr) == 'X(q(0))*X(q(1))'
paulisum1 = cirq.X(q[0]) * cirq.X(q[1]) + 4
assert str(paulisum1) == '1.000*X(q0)*X(q1)+4.000*I'
assert str(paulisum1) == '1.000*X(q(0))*X(q(1))+4.000*I'
paulisum2 = cirq.X(q[0]) * cirq.X(q[1]) + cirq.Z(q[0])
assert str(paulisum2) == '1.000*X(q0)*X(q1)+1.000*Z(q0)'
assert str(paulisum2) == '1.000*X(q(0))*X(q(1))+1.000*Z(q(0))'
paulisum3 = cirq.X(q[0]) * cirq.X(q[1]) + cirq.Z(q[0]) * cirq.Z(q[1])
assert str(paulisum3) == '1.000*X(q0)*X(q1)+1.000*Z(q0)*Z(q1)'
assert f"{paulisum3:.0f}" == '1*X(q0)*X(q1)+1*Z(q0)*Z(q1)'
assert str(paulisum3) == '1.000*X(q(0))*X(q(1))+1.000*Z(q(0))*Z(q(1))'
assert f"{paulisum3:.0f}" == '1*X(q(0))*X(q(1))+1*Z(q(0))*Z(q(1))'

empty = cirq.PauliSum.from_pauli_strings([])
assert str(empty) == "0.000"
Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/ops/pauli_string_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ def text(self, to_print):

p = FakePrinter()
result._repr_pretty_(p, False)
assert p.text_pretty == 'X(q0)*Y(q1)*Z(q2)'
assert p.text_pretty == 'X(q(0))*Y(q(1))*Z(q(2))'

# Test cycle handling
p = FakePrinter()
Expand Down Expand Up @@ -1874,7 +1874,7 @@ def test_mutable_pauli_string_dict_pauli_like_not_pauli_like():
def test_mutable_pauli_string_text():
p = cirq.MutablePauliString(cirq.X(cirq.LineQubit(0)) * cirq.Y(cirq.LineQubit(1)))
assert str(cirq.MutablePauliString()) == "mutable I"
assert str(p) == "mutable X(q0)*Y(q1)"
assert str(p) == "mutable X(q(0))*Y(q(1))"
cirq.testing.assert_equivalent_repr(p)


Expand Down
4 changes: 2 additions & 2 deletions cirq-core/cirq/ops/pauli_sum_exponential_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ def test_pauli_sum_exponential_repr(psum_exp):
(cirq.PauliSumExponential(0, np.pi / 2), 'exp(j * 1.5707963267948966 * (0.000))'),
(
cirq.PauliSumExponential(2j * cirq.X(q0) + 4j * cirq.Y(q1), 2),
'exp(2 * (2.000j*X(q0)+4.000j*Y(q1)))',
'exp(2 * (2.000j*X(q(0))+4.000j*Y(q(1))))',
),
(
cirq.PauliSumExponential(0.5 * cirq.X(q0) + 0.6 * cirq.Y(q1), sympy.Symbol("theta")),
'exp(j * theta * (0.500*X(q0)+0.600*Y(q1)))',
'exp(j * theta * (0.500*X(q(0))+0.600*Y(q(1))))',
),
),
)
Expand Down
22 changes: 11 additions & 11 deletions cirq-core/cirq/sim/clifford/clifford_simulator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@ def test_run_no_repetitions():
simulator = cirq.CliffordSimulator()
circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0))
result = simulator.run(circuit, repetitions=0)
assert sum(result.measurements['q0']) == 0
assert sum(result.measurements['q(0)']) == 0


def test_run_hadamard():
q0 = cirq.LineQubit(0)
simulator = cirq.CliffordSimulator()
circuit = cirq.Circuit(cirq.H(q0), cirq.measure(q0))
result = simulator.run(circuit, repetitions=100)
assert sum(result.measurements['q0'])[0] < 80
assert sum(result.measurements['q0'])[0] > 20
assert sum(result.measurements['q(0)'])[0] < 80
assert sum(result.measurements['q(0)'])[0] > 20


def test_run_GHZ():
(q0, q1) = (cirq.LineQubit(0), cirq.LineQubit(1))
simulator = cirq.CliffordSimulator()
circuit = cirq.Circuit(cirq.H(q0), cirq.H(q1), cirq.measure(q0))
result = simulator.run(circuit, repetitions=100)
assert sum(result.measurements['q0'])[0] < 80
assert sum(result.measurements['q0'])[0] > 20
assert sum(result.measurements['q(0)'])[0] < 80
assert sum(result.measurements['q(0)'])[0] > 20


def test_run_correlations():
Expand All @@ -50,7 +50,7 @@ def test_run_correlations():
circuit = cirq.Circuit(cirq.H(q0), cirq.CNOT(q0, q1), cirq.measure(q0, q1))
for _ in range(10):
result = simulator.run(circuit)
bits = result.measurements['q0,q1'][0]
bits = result.measurements['q(0),q(1)'][0]
assert bits[0] == bits[1]


Expand Down Expand Up @@ -152,7 +152,7 @@ def test_run_measure_multiple_qubits():
circuit.append(cirq.X(q1))
circuit.append(cirq.measure(q0, q1))
result = simulator.run(circuit, repetitions=3)
np.testing.assert_equal(result.measurements, {'q0,q1': [[b0, b1]] * 3})
np.testing.assert_equal(result.measurements, {'q(0),q(1)': [[b0, b1]] * 3})


def test_simulate_moment_steps():
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_simulate_moment_steps_intermediate_measurement(split):
simulator = cirq.CliffordSimulator(split_untangled_states=split)
for i, step in enumerate(simulator.simulate_moment_steps(circuit)):
if i == 1:
result = int(step.measurements['q0'][0])
result = int(step.measurements['q(0)'][0])
expected = np.zeros(2)
expected[result] = 1
np.testing.assert_almost_equal(step.state.to_numpy(), expected)
Expand Down Expand Up @@ -392,8 +392,8 @@ def test_clifford_circuit_2(qubits, split):
circuit.append(cirq.measure(qubits[0]))
result = cirq.CliffordSimulator(split_untangled_states=split).run(circuit, repetitions=100)

assert sum(result.measurements['q0'])[0] < 80
assert sum(result.measurements['q0'])[0] > 20
assert sum(result.measurements['q(0)'])[0] < 80
assert sum(result.measurements['q(0)'])[0] > 20


@pytest.mark.parametrize('split', [True, False])
Expand Down Expand Up @@ -549,7 +549,7 @@ def test_valid_apply_measurement():
state = cirq.CliffordState(qubit_map={q0: 0}, initial_state=1)
measurements = {}
_ = state.apply_measurement(cirq.measure(q0), measurements, np.random.RandomState())
assert measurements == {'q0': [1]}
assert measurements == {'q(0)': [1]}


@pytest.mark.parametrize('split', [True, False])
Expand Down
6 changes: 3 additions & 3 deletions cirq-core/cirq/sim/clifford/stabilizer_sampler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def test_reset():
q = cirq.LineQubit(0)
sampler = cirq.StabilizerSampler()
c = cirq.Circuit(cirq.X(q), cirq.reset(q), cirq.measure(q))
assert sampler.sample(c)['q0'][0] == 0
assert sampler.sample(c)['q(0)'][0] == 0
c = cirq.Circuit(cirq.H(q), cirq.reset(q), cirq.measure(q))
assert sampler.sample(c)['q0'][0] == 0
assert sampler.sample(c)['q(0)'][0] == 0
c = cirq.Circuit(cirq.reset(q), cirq.measure(q))
assert sampler.sample(c)['q0'][0] == 0
assert sampler.sample(c)['q(0)'][0] == 0
4 changes: 2 additions & 2 deletions cirq-core/cirq/sim/clifford/stabilizer_state_ch_form_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,5 @@ def test_run():
)
cirq.act_on(op, args)
measurements = {str(k): list(v[-1]) for k, v in classical_data.records.items()}
assert measurements['q1'] == [1]
assert measurements['q0'] != measurements['q2']
assert measurements['q(1)'] == [1]
assert measurements['q(0)'] != measurements['q2']
Loading