Skip to content

[BUG] Depth calculation for external gates #77

@TheGupta2012

Description

@TheGupta2012

Environment

  • PyQASM version: source
  • Python version: 3.12.7
  • Operating system: macOS 14.4

What happened?

With the merging of #59 , we need to update the depth calculation of the QasmModule. As mentioned in the PR, when a module is unrolled with external gates, the depth calculation implicitly refers the gate definition to calculate the final depth. See unrolling example in external gates PR.

Suggestions (Optional)

  • As stated, we can treat any external_gate as "atomic" whenever we are calculating the depth of the quantum program. Moreover, we need to update the bit depths inside the _visit_external_gate_operation method after broadcasting it on the qubits arguments.

  • Note that the validation steps for custom gate and basic gate already have depth calculations inside their definitions. We'll either need to disable them OR think of how we want to define depth in the first place.

  • By the latter, I mean that do we want to include gate definitions of external gates in depth calculations or not? Since we force the gate to be defined (by the user or as a basic gate), we are specifying its depth through the definition.

There are two approaches to handle this -

  1. Depth is dependent on external gate definitions: We calculate the depth of the circuit as defined by the user gate definitions. Even though a person specifies an external gate, the depth is calculated by considering its definition -
In [6]: qasm_str = """
   ...: OPENQASM 3.0;
   ...: include "stdgates.inc";
   ...: gate custom_native_gate q1, q2 {
   ...:   h q1;
   ...:   cx q1, q2;
   ...:   h q2;
   ...: }
   ...: qubit[2] q;
   ...: custom_native_gate q[0], q[1];
   ...: """

In [7]: mod = pyqasm.loads(qasm_str)

In [8]: mod.unroll(external_gates = ["custom_native_gate"])

In [9]: mod.depth()
Out[9]: 3

A corner case is when the body is empty. Here the depth should be incremented by 1 but because of the empty body, it will just be 0 -

In [1]: import pyqasm

In [2]: qasm_str = """
   ...: OPENQASM 3.0;
   ...: include "stdgates.inc";
   ...: gate custom_native_gate q1, q2 { }
   ...: qubit[2] q;
   ...: custom_native_gate q[0], q[1];
   ...: """

In [3]: mod = pyqasm.loads(qasm_str)

In [4]: mod.unroll(external_gates = ["custom_native_gate"])

In [5]: mod.depth()
Out[5]: 0

In this case, we will add 1 to the depth while calculating it inside the external gate handler.

  1. Depth is independent of external definitions: We ignore the external gates' definitions and treat them as gates with empty body. This means -
gate custom_native_gate q1, q2 {
   h q1;
   cx q1, q2;
   h q2;
 }

is treated as -

gate custom_native_gate q1, q2 { }

This will not affect the depth calculations as there won't be any statement to visit while processing this external gate. Then, at the end, we just update the depths of qubits appropriately.

Happy to discuss this more @Sola85 and @ryanhill1

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingqasm3Related to openqasm3unitary-hackIssues for the Unitary Hack

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions