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

DAG.width needs to be updated to match QuantumCircuit definition #2564

Closed
nonhermitian opened this issue Jun 3, 2019 · 11 comments · Fixed by #2812
Closed

DAG.width needs to be updated to match QuantumCircuit definition #2564

nonhermitian opened this issue Jun 3, 2019 · 11 comments · Fixed by #2812
Assignees
Labels
good first issue Good for newcomers status: pending PR It has one or more PRs pending to solve this issue
Milestone

Comments

@nonhermitian
Copy link
Contributor

Information

  • Qiskit Terra version: master
  • Python version:
  • Operating system:

What is the current behavior?

qr = QuantumRegister(5,'qr')
cr = ClassicalRegister(5, 'cr')
ghz = QuantumCircuit(qr, cr, name='ghz')

ghz.h(qr[2])
ghz.cx(qr[2], qr[1])
ghz.cx(qr[1], qr[0])
ghz.cx(qr[2], qr[3])
ghz.cx(qr[3], qr[4])
ghz.draw()

ghz_dag = circuit_to_dag(ghz)

print(ghz.width(), ghz_dag.width())

gives: (10, 5).

Steps to reproduce the problem

What is the expected behavior?

Suggested solutions

DAG width needs to count both qubits and cbits for self-consistency.

@kdk kdk added this to the 0.9 milestone Jun 11, 2019
@maddy-tod maddy-tod added the good first issue Good for newcomers label Jun 20, 2019
@jwoehr
Copy link
Contributor

jwoehr commented Jun 22, 2019

qiskit.dagcircuit.dagcircuit defines width() as

    def width(self):
        """Return the total number of qubits used by the circuit."""
        return len(self.wires) - self.num_cbits()

There may be code that depends on the semantic of width() == number of qubits

@jwoehr
Copy link
Contributor

jwoehr commented Jun 28, 2019

Fulfilling the literal request of this ticket would be as simple as removing the - self.num_cbits() as shown in my previous comment. But obviously the coder believed that width means number of qubits. How to resolve this? Just change the code? Look for usages?

@jwoehr
Copy link
Contributor

jwoehr commented Jun 28, 2019

The only test of dagcircuit.width() I have found is in qiskit-terra/test/python/test_dagcircuit.py and it doesn't set any cbits.

class TestCircuitProperties(QiskitTestCase):
    """DAGCircuit properties test."""

    def setUp(self):
        qr1 = QuantumRegister(4)
        qr2 = QuantumRegister(2)
        circ = QuantumCircuit(qr1, qr2)
        circ.h(qr1[0])
        circ.cx(qr1[2], qr1[3])
        circ.h(qr1[2])
        circ.t(qr1[2])
        circ.ch(qr1[2], qr1[1])
        circ.u2(0.1, 0.2, qr1[3])
        circ.ccx(qr2[0], qr2[1], qr1[0])

        self.dag = circuit_to_dag(circ)

...

    def test_circuit_width(self):
        """Test number of qubits in circuit."""
        self.assertEqual(self.dag.width(), 6)

However, in qiskit/transpiler/passes/width.py the dagcircuit.width() member function seems to be used as-is, that is, only counting qubits:

from qiskit.transpiler.basepasses import AnalysisPass


class Width(AnalysisPass):
    """ An analysis pass for calculating the width of a DAG circuit.
    """

    def run(self, dag):
        self.property_set['width'] = dag.width()

So does changing qiskit.dagcircuit.dagcircuit.width() to count cbits as well break the transpiler?

@nonhermitian
Copy link
Contributor Author

It probably breaks several things which is why it has not been implemented yet.

@jwoehr
Copy link
Contributor

jwoehr commented Jun 28, 2019

Ah, so maybe the trick is to

  1. Define dagcircuit.qubit_width()
  2. Sift through all the verticals looking for current invocations of dagcircuit.width() and change them to dagcircuit.qubit_width()

??

@jwoehr
Copy link
Contributor

jwoehr commented Jun 30, 2019

First step: coded replacement function.

Next, have to look everywhere for usage of DAGCircuit.width() and replace with DAGCircuit.num_qbits().

Then when invocations are all changed, DAGCircuit.width() can be changed to return the total number of bits.

@jwoehr
Copy link
Contributor

jwoehr commented Jul 16, 2019

Per @ajavadia 's comments after merge of #2708 I'll proceed to

  1. Search for invocations or mentions of DAGCircuit.num_cbits() and change to DAGCircuit.num_clbits()
  • qiskit-tutorials/qiskit/terra/advanced_circuits.ipynb
  • qiskit-aqua/qiskit/aqua/utils/circuit_utils.py
  • qiskit/docs/terra/custom_gates.rst
  1. Remove DAGCircuit.num_cbits()
  2. Search for current usage of DAGCircuit.width() and replace with DAGCircuit.num_qbits()
  3. After such invocations are changed, DAGCircuit.width() can be changed to return the total number of bits (qubits + clbits) fulfilling the intent of this issue.

@jwoehr
Copy link
Contributor

jwoehr commented Jul 17, 2019

The pull requests #2807 and qiskit-aqua number 613 should complete this issue.

@jwoehr
Copy link
Contributor

jwoehr commented Jul 19, 2019

It's now pull requests #2812 and aqua pr 614

@ajavadia ajavadia added the status: pending PR It has one or more PRs pending to solve this issue label Jul 22, 2019
@jwoehr
Copy link
Contributor

jwoehr commented Jul 22, 2019

I'll make the transpiler pass changes requested in #2812 on 2019-07-22

@jwoehr
Copy link
Contributor

jwoehr commented Jul 22, 2019

I have pushed the transpiler pass changes requested in #2812

@kdk kdk closed this as completed in #2812 Aug 1, 2019
kdk pushed a commit that referenced this issue Aug 1, 2019
* updated readme to #2564 changes

* typographic considerations for change log

* This should complete #2564

* remove now-superfluous DAGCircuit.num_cbits() which was replaced by
DAGCircuit.num_clbits()

* - Reverted Width transpiler pass to return DAGCircuit.width()
  Note that is the new width() which is qubits + clbits
- Added a NumQutbits transpiler pass to return that which the
  Width pass was previously returning, i.e., DAGCircuit.num_qubits

* change requested on pull request #2812 by Kevin Krsulich

* Changed test as requested in pull request conversation

* Added NumQubits() to test as requested in pull request discussion
faisaldebouni pushed a commit to faisaldebouni/qiskit-terra that referenced this issue Aug 5, 2020
* updated readme to Qiskit#2564 changes

* typographic considerations for change log

* This should complete Qiskit#2564

* remove now-superfluous DAGCircuit.num_cbits() which was replaced by
DAGCircuit.num_clbits()

* - Reverted Width transpiler pass to return DAGCircuit.width()
  Note that is the new width() which is qubits + clbits
- Added a NumQutbits transpiler pass to return that which the
  Width pass was previously returning, i.e., DAGCircuit.num_qubits

* change requested on pull request Qiskit#2812 by Kevin Krsulich

* Changed test as requested in pull request conversation

* Added NumQubits() to test as requested in pull request discussion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers status: pending PR It has one or more PRs pending to solve this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants