Repository: PennyLaneAI/pennylane
Pull request #4951 (2023-12-15 09:24:47)

Allow conversion from ApproxTimeEvolution gates to openqasm

**Context:**

We update the `depth` parameter in to call to `expand` in `to_openqasm`. This allows for conversion of the `ApproxTimeEvolution` (which includes a MultiRZ) to openqasm.

**Description of the Change:**

Updating the depth parameters converts the `MultiRZ` into a series of `CNOT` and `RZ` gates. An alternative would be to specialize the conversion of `MultiRZ` inside the `to_openqasm` method.

**Benefits:**

Circuits with `ApproxTimeEvolution` can be converted to openqasm.

**Possible Drawbacks:**

Perhaps more conversions are executed for conversions to openqasm.

**Related GitHub Issues:**


codecov[bot]: ## [Codecov](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4951?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`1e22d48`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/commit/1e22d48691e8ec8dd11817dff66f12cd6fcdafb1?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.66% compared to head [(`28ba33c`)](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4951?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI) 99.49%.
> Report is 14 commits behind head on master.


<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #4951      +/-   ##
==========================================
- Coverage   99.66%   99.49%   -0.18%     
==========================================
  Files         388      388              
  Lines       35339    35245      -94     
==========================================
- Hits        35222    35066     -156     
- Misses        117      179      +62     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/PennyLaneAI/pennylane/pull/4951?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=PennyLaneAI).


Patch:
diff --git a/doc/releases/changelog-dev.md b/doc/releases/changelog-dev.md
index 089a9ec66..46a4771e5 100644
--- a/doc/releases/changelog-dev.md
+++ b/doc/releases/changelog-dev.md
@@ -658,6 +658,10 @@
 * `qml.map_wires` no longer fails when mapping nested quantum tapes.
   [(#4901)](https://github.com/PennyLaneAI/pennylane/pull/4901)
 
+* Conversion of circuits to openqasm now decomposes to a depth of 10, allowing support 
+  for operators requiring more than 2 iterations of decomposition, such as the `ApproxTimeEvolution` gate.
+  [(#4951)](https://github.com/PennyLaneAI/pennylane/pull/4951)
+  
 <h3>Contributors ✍️</h3>
 
 This release contains contributions from (in alphabetical order):
@@ -671,6 +675,7 @@ Astral Cai,
 Minh Chau,
 Isaac De Vlugt,
 Amintor Dusko,
+Pieter Eendebak,
 Lillian Frederiksen,
 Pietropaolo Frisoni,
 Josh Izaac,
diff --git a/pennylane/tape/qscript.py b/pennylane/tape/qscript.py
index e10957c67..60efd2791 100644
--- a/pennylane/tape/qscript.py
+++ b/pennylane/tape/qscript.py
@@ -1120,7 +1120,7 @@ class QuantumScript:
         # pylint: disable=no-member
         just_ops = QuantumScript(operations)
         operations = just_ops.expand(
-            depth=2, stop_at=lambda obj: obj.name in OPENQASM_GATES
+            depth=10, stop_at=lambda obj: obj.name in OPENQASM_GATES
         ).operations
 
         # create the QASM code representing the operations


Test patch:
diff --git a/tests/circuit_graph/test_qasm.py b/tests/circuit_graph/test_qasm.py
index a49224244..9521b1c8b 100644
--- a/tests/circuit_graph/test_qasm.py
+++ b/tests/circuit_graph/test_qasm.py
@@ -98,6 +98,35 @@ class TestToQasmUnitTests:
 
         assert res == expected
 
+    def test_to_ApproxTimeEvolution(self):
+        """Test for case that requires a decomposition depth of 3 to successfully convert to a MutliRZ"""
+        H = qml.Hamiltonian(
+            [
+                1,
+            ],
+            [
+                qml.PauliZ(0) @ qml.PauliZ(1),
+            ],
+        )
+        circuit = qml.tape.QuantumScript([qml.ApproxTimeEvolution(H, 1, n=1)])
+        res = circuit.to_openqasm(wires=Wires([0, 1]))
+
+        expected = dedent(
+            """\
+            OPENQASM 2.0;
+            include "qelib1.inc";
+            qreg q[2];
+            creg c[2];
+            cx q[1],q[0];
+            rz(2.0) q[0];
+            cx q[1],q[0];
+            measure q[0] -> c[0];
+            measure q[1] -> c[1];
+            """
+        )
+
+        assert res == expected
+
     def test_rotation_gate_decomposition(self):
         """Test that gates not natively supported by QASM, such as the
         rotation gate, are correctly decomposed and serialized."""