Skip to content

Commit

Permalink
simulator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daxfohl committed Jan 19, 2022
1 parent 50c4e20 commit d69684a
Showing 1 changed file with 36 additions and 130 deletions.
166 changes: 36 additions & 130 deletions cirq-core/cirq/optimizers/deferred_measurements_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,118 +16,64 @@
from cirq.optimizers.deferred_measurements import defer_measurements


def test_diagram():
def assert_equivalent_to_deferred(circuit: cirq.Circuit):
qubits = circuit.all_qubits()
deferred = defer_measurements(circuit)
keys = cirq.measurement_key_objs(circuit)
sim = cirq.Simulator()
for i in range(2**len(qubits)):
result = sim.simulate(circuit, initial_state=i).measurements
result1 = sim.simulate(deferred, initial_state=2**len(keys)*i).measurements
assert result == result1


def test_basic():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(cirq.measure(q0, key='a'), cirq.X(q1).with_classical_controls('a'))

cirq.testing.assert_has_diagram(
defer_measurements(circuit),
"""
0: ─────@────────────────
1: ─────┼───X────────────
│ │
a 0: ───X───@───M('a')───
""",
use_unicode_characters=True,
)
assert_equivalent_to_deferred(circuit)


def test_diagram_extra_measurements():
def test_extra_measurements():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.measure(q0, key='a'),
cirq.measure(q0, key='b'),
cirq.X(q1).with_classical_controls('a'),
)
assert_equivalent_to_deferred(circuit)

cirq.testing.assert_has_diagram(
defer_measurements(circuit),
"""
┌──┐
0: ─────@────@──────────────
│ │
1: ─────┼────┼X─────────────
│ ││
a 0: ───X────┼@────M('a')───
b 0: ────────X─────M('b')───
└──┘
""",
use_unicode_characters=True,
)


def test_diagram_extra_controlled_bits():
def test_extra_controlled_bits():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.measure(q0, key='a'),
cirq.CX(q0, q1).with_classical_controls('a'),
)
assert_equivalent_to_deferred(circuit)

cirq.testing.assert_has_diagram(
defer_measurements(circuit),
"""
0: ─────@───@────────────
│ │
1: ─────┼───X────────────
│ │
a 0: ───X───@───M('a')───
""",
use_unicode_characters=True,
)


def test_diagram_extra_control_bits():
def test_extra_control_bits():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.measure(q0, key='a'),
cirq.measure(q0, key='b'),
cirq.X(q1).with_classical_controls('a', 'b'),
)

cirq.testing.assert_has_diagram(
defer_measurements(circuit),
"""
0: ─────@───@────────────────
│ │
1: ─────┼───┼───X────────────
│ │ │
a 0: ───X───┼───@───M('a')───
│ │
b 0: ───────X───@───M('b')───
""",
use_unicode_characters=True,
)
assert_equivalent_to_deferred(circuit)


def test_diagram_multiple_ops_single_moment():
def test_multiple_ops_single_moment():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.measure(q0, key='a'),
cirq.measure(q1, key='b'),
cirq.X(q0).with_classical_controls('a'),
cirq.X(q1).with_classical_controls('b'),
)

cirq.testing.assert_has_diagram(
defer_measurements(circuit),
"""
┌──┐ ┌──┐
0: ──────@──────X──────────────
│ │
1: ──────┼@─────┼X─────────────
││ ││
a 0: ────X┼─────@┼────M('a')───
│ │
b 1: ─────X──────@────M('b')───
└──┘ └──┘
""",
use_unicode_characters=True,
)
assert_equivalent_to_deferred(circuit)


def test_diagram_subcircuit():
def test_subcircuit():
q0, q1 = cirq.LineQubit.range(2)
circuit = cirq.Circuit(
cirq.CircuitOperation(
Expand All @@ -137,22 +83,7 @@ def test_diagram_subcircuit():
)
)
)

cirq.testing.assert_has_diagram(
defer_measurements(circuit),
"""
[ 0: ─────@─────── ]
[ │ ]
0: ─────[ 1: ─────┼───X─── ]────────────
[ │ │ ]
[ a 0: ───X───@─── ]
1: ─────#2──────────────────────────────
a 0: ───#3─────────────────────M('a')───
""",
use_unicode_characters=True,
)
assert_equivalent_to_deferred(circuit)


def test_scope_local():
Expand All @@ -163,43 +94,18 @@ def test_scope_local():
)
middle = cirq.Circuit(cirq.CircuitOperation(inner.freeze(), repetitions=2))
outer_subcircuit = cirq.CircuitOperation(middle.freeze(), repetitions=2)
assert not cirq.control_keys(outer_subcircuit)
cirq.testing.assert_has_diagram(
defer_measurements(cirq.Circuit(outer_subcircuit)),
"""
[ [ 0: ─────@───X─── ] ]
[ 0: ───────[ │ │ ]──────────── ]
[ [ a 0: ───X───@─── ](loops=2) ]
0: ─────────[ │ ]─────────────────────────
[ 0:a 0: ───#2────────────────────────────── ]
[ │ ]
[ 1:a 0: ───#3────────────────────────────── ](loops=2)
0:0:a 0: ───#2────────────────────────────────────────────────────────M('0:0:a')───
0:1:a 0: ───#3────────────────────────────────────────────────────────M('0:1:a')───
1:0:a 0: ───#4────────────────────────────────────────────────────────M('1:0:a')───
1:1:a 0: ───#5────────────────────────────────────────────────────────M('1:1:a')───
""",
use_unicode_characters=True,
assert_equivalent_to_deferred(cirq.Circuit(outer_subcircuit))


def test_scope_extern():
q = cirq.LineQubit(0)
inner = cirq.Circuit(
cirq.measure(q, key='a'),
cirq.X(q).with_classical_controls('b'),
)
circuit = cirq.Circuit(outer_subcircuit)
s = defer_measurements(circuit).freeze()
cirq.testing.assert_has_diagram(
cirq.CircuitOperation(s).mapped_circuit(deep=True),
"""
0: ─────────@───X───@───X───@───X───@───X────────────────
│ │ │ │ │ │ │ │
0:0:a 0: ───X───@───┼───┼───┼───┼───┼───┼───M('0:0:a')───
│ │ │ │ │ │
0:1:a 0: ───────────X───@───┼───┼───┼───┼───M('0:1:a')───
│ │ │ │
1:0:a 0: ───────────────────X───@───┼───┼───M('1:0:a')───
│ │
1:1:a 0: ───────────────────────────X───@───M('1:1:a')───
""",
use_unicode_characters=True,
middle = cirq.Circuit(
cirq.measure(q, key=cirq.MeasurementKey('b')),
cirq.CircuitOperation(inner.freeze(), repetitions=2),
)
assert circuit == cirq.Circuit(cirq.decompose(outer_subcircuit))
outer_subcircuit = cirq.CircuitOperation(middle.freeze(), repetitions=2)
assert_equivalent_to_deferred(cirq.Circuit(outer_subcircuit))

0 comments on commit d69684a

Please sign in to comment.