Skip to content

Commit f066e63

Browse files
authored
Fix EngineProgram.get_circuit(_async) empty code detection (#7850)
1 parent 2ef9f37 commit f066e63

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

cirq-google/cirq_google/engine/engine_program.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,9 @@ async def get_circuit_async(self) -> cirq.Circuit:
361361
Returns:
362362
The program's cirq Circuit.
363363
"""
364-
if self._program is None or self._program.code is None:
364+
# The code field is an any_pb2.Any and is always set. But if the program has not
365+
# been fetched this field may be empty, which we can see by checking the type_url.
366+
if self._program is None or not self._program.code or not self._program.code.type_url:
365367
self._program = await self.context.client.get_program_async(
366368
self.project_id, self.program_id, True
367369
)

cirq-google/cirq_google/engine/engine_program_test.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,12 +298,14 @@ def test_get_circuit_v1(get_program_async):
298298

299299

300300
@mock.patch('cirq_google.engine.engine_client.EngineClient.get_program_async')
301-
def test_get_circuit_v2(get_program_async):
301+
@pytest.mark.parametrize("include_empty_program", [False, True])
302+
def test_get_circuit_v2(get_program_async, include_empty_program: bool) -> None:
302303
circuit = cirq.Circuit(
303304
cirq.X(cirq.GridQubit(5, 2)) ** 0.5, cirq.measure(cirq.GridQubit(5, 2), key='result')
304305
)
305306

306-
program = cg.EngineProgram('a', 'b', EngineContext())
307+
program_msg = quantum.QuantumProgram() if include_empty_program else None
308+
program = cg.EngineProgram('a', 'b', EngineContext(), _program=program_msg)
307309
get_program_async.return_value = quantum.QuantumProgram(code=_PROGRAM_V2)
308310
cirq.testing.assert_circuits_with_terminal_measurements_are_equivalent(
309311
program.get_circuit(), circuit

0 commit comments

Comments
 (0)