Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Update the depth calculation to include branching operations with classical registers #53

Open
TheGupta2012 opened this issue Oct 29, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@TheGupta2012
Copy link
Contributor

Feature Description

Qiskit Version: 1.2.0

Quantum programs with branching conditions containing classical registers should be considered for depth calculations. For example, the following circuit -

In [41]: from qiskit import QuantumCircuit

In [42]: qasm_str = """
    ...: OPENQASM 2.0;
    ...: include "qelib1.inc";
    ...: qreg q[2];
    ...: creg c0[1];
    ...: creg c1[1];
    ...: h q[0];
    ...: h q[1];
    ...: measure q[0] -> c0[0];
    ...: if(c0==0) x q[1];
    ...: """

In [43]: qc = QuantumCircuit.from_qasm_str(qasm_str)

In [44]: qc.draw()
Out[44]:
      ┌───┐┌─┐
 q_0: ┤ H ├┤M├───────
      ├───┤└╥┘ ┌───┐
 q_1: ┤ H ├─╫──┤ X ├─
      └───┘ ║  └─╥─┘
            ║ ┌──╨──┐
c0: 1/══════╩═╡ 0x00 └─────┘
c1: 1/═══════════════

In [45]: qc.depth()
Out[45]: 3

has a depth of 3 whereas pyqasm gives a depth of 2 -

In [50]: import pyqasm

In [51]: qasm_str = """
    ...: OPENQASM 2.0;
    ...: include "qelib1.inc";
    ...: qreg q[2];
    ...: creg c0[1];
    ...: creg c1[1];
    ...: h q[0];
    ...: h q[1];
    ...: measure q[0] -> c0[0];
    ...: if(c0==0) x q[1];
    ...: """

In [52]: pyqasm.load(qasm_str).depth()
Out[52]: 2

This is because the classical register depth is not considered during the calculation of qubit depths inside the if block. The depth of c0_0 is 2 when it is used to condition the X gate on q_1.

Implementation (Optional)

  • While unrolling the branching conditions, we look at whether the condition contains a set of classical register bits or not. We need to update the depth of all these classical bits to the max depth of this set of bits, eg. -
In [75]: qc.draw(cregbundle=False)
Out[75]:
     ┌─┐     ┌─┐┌─┐
q_0: ┤M├──■──┤M├┤M├─────
     └╥┘┌─┴─┐└╥┘└╥┘
q_1: ─╫─┤ X ├─╫──╫──────
      ║ └───┘ ║  ║ ┌───┐
q_2: ─╫───────╫──╫─┤ H ├
      ║       ║  ║ └─╥─┘
c_0: ═╩═══════╬══╬═══╬══
              ║  ║   ║
c_1: ═════════╩══╬═══╬══
                 ║   ║
c_2: ════════════╩═══■══

c_3: ═══════════════════


In [76]: qc.depth()
Out[76]: 5

Here, if c_0, c_1 and c_2 were used in a branching statement, the depth of each bit would be set to 4 as max depth of bits i.e. c_2 is 4. In above example, since c_2 is used for the classically conditioned H gate, the depth of the circuit is supposed to be 5 but is coming out as 4 with pyqasm -

In [82]: import pyqasm

In [83]: qasm_str = 'OPENQASM 3.0;\ninclude "stdgates.inc";\nbit[4] c;\nqubit[3] q;\nc[0] = m
    ...: easure q[0];\ncx q[0], q[1];\nc[1] = measure q[0];\nc[2] = measure q[0];\nif (c[2])
    ...: {\n  h q[2];\n}\n'

In [84]: pyqasm.load(qasm_str).depth()
Out[84]: 4
  • It is not entirely clear how this "max depth of involved classical bits" will be trickled down to the qubits of the if block. A brute force approach is used inside the qbraid sdk's depth function but might not work for a recursive approach used in QasmVisitor.
@TheGupta2012 TheGupta2012 added the enhancement New feature or request label Oct 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant