Repository navigation
Add _qasm_ transpilation to CircuitOperation, and use it in If - #8418
Acciaccatura wants to merge 4 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8418 +/- ##
=======================================
Coverage 99.59% 99.59%
=======================================
Files 1133 1133
Lines 103919 103936 +17
=======================================
+ Hits 103498 103515 +17
Misses 421 421 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
mhucka
left a comment
There was a problem hiding this comment.
@Acciaccatura Thank you for working on this, and i'm sorry it took so long to review this. Here's a preliminary set of comments. I think there are a couple of other places that may need changes, but it would be best to merge the branch and address these initial points, and then there can be another round of reviews.
| if isinstance(self._sub_operation, CircuitOperation): | ||
| subop_qasm_body = ''.join(f' {line}\n' for line in subop_qasm.strip().split('\n')) | ||
| subop_qasm = f'{{\n{subop_qasm_body}}}' |
There was a problem hiding this comment.
I think this will not produce syntactically correct output in openqasm 2.0. It will produce if (m_a==1) {\n x q[1];\n z q[1];\n}, but the { ... } are not part of openqasm 2.0.
It may be worth looking at how ClassicallyControlledOperation.qasm handles multi-statement conditional QASM output.
| return None | ||
| from cirq.circuits import CircuitOperation | ||
|
|
||
| if isinstance(self._sub_operation, CircuitOperation): |
There was a problem hiding this comment.
I think this currently will miss multi-statement single operations and also not handle 0- or 1-operation CircuitOperations correctly. A single gate operation whose _qasm_ emits multiple statements, such as cirq.If('a', cirq.CCZ(q0, q1, q2)) or cirq.If('a', cirq.CCY(q0, q1, q2)), is a GateOperation, not a CircuitOperation. Using isinstance(self._sub_operation, CircuitOperation), the If('a', cirq.CCZ(q0, q1, q2)) will leave the 2nd and 3rd statements unguarded; i.e., if (m_a==1) h q[2];\nccx q[0],q[1],q[2];\nh q[2];\n), reproducing #8329.
Conversely, cirq.If('a', [cirq.X(q1)]) will wrap the 1-element list in a CircuitOperation and emit braces, whereas cirq.If('a', cirq.X(q1)) will not. And an empty cirq.If('a', []) (which has subop_qasm == "") will end up with "".strip().split('\n'), which is [''], which produces 'if (m_a==1) {\n \n}' instead of "".
| # TODO: Implement transpilation for additional controls. | ||
| if self.repetitions != 1 or self.repeat_until is not None: | ||
| return None # pragma: no cover | ||
| return ''.join(protocols.qasm(op, args=args, qubits=qubits) for op in self._decompose_()) |
There was a problem hiding this comment.
Whoops, this is problematic. CircuitOperation._qasm_ will end up raising TypeError instead of returning None when a sub-operation needs decomposition or does not directly implement _qasm_. This will break existing QasmOutput decomposition.
If self._decompose_() contains any operation whose _qasm_ is unimplemented or returns None to request decomposition, such as cirq.CNOT(q0, q1).controlled_by(q2)) or a 2-qubit MatrixGate, the protocols.qasm(op, ...) will raise TypeError.
To fix it, an approach may be to query each decomposed operation with default=None and return None if any sub-operation cannot be directly formatted to QASM (allowing QasmOutput to fall back to full decomposition).
hello! while working on #8839, it seems like
CircuitOperationmay be used more and more to represent circuit control flow bodies of more than one operation (e.g. below):Cirq/cirq-core/cirq/ops/if_op.py
Lines 92 to 100 in 1403830
right now,
CircuitOperationdoes not implement the QASM protocol, so any control flow operator will fail to transpile to QASM unless they explicitly handle transpilingCircuitOperations themselves. maybe it would be useful to just implement this protocol to avoid some code deduplication? in this PR:CircuitOperationimplements_qasm_, which simply outputs its contents decomposed. ex:becomes:
Ifis updated to conditionally show braces when_sub_operationis aCircuitOperation. ex:becomes: