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

Add separable_circuits method to DAGCircuit class #9471

Merged
merged 29 commits into from
Jul 19, 2023
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c7ef19b
Add decompose_dag method to DAGCircuit class
caleb-johnson Jan 25, 2023
e153363
Don't clean up idle wires within function
caleb-johnson Jan 26, 2023
89633c7
Add tests for DAGCircuit.separable_circuits
caleb-johnson Jan 31, 2023
8988448
Add ordering and QuantumCircuit equivalency tests. Add release note.
caleb-johnson Feb 1, 2023
761a9e9
Code style changes
caleb-johnson Feb 1, 2023
1e91faa
Update release note
caleb-johnson Feb 1, 2023
6a1206e
Merge branch 'main' into disconnected-components
caleb-johnson Feb 1, 2023
235d618
Clean up documentation
caleb-johnson Feb 15, 2023
925481f
Return a DAG for any partition with at least one qubit, even if empty
caleb-johnson Feb 23, 2023
2df57c3
Merge main into branch
caleb-johnson Jun 8, 2023
a890b11
Adjust tests to new flag
caleb-johnson Jun 9, 2023
af05ec5
merge main
caleb-johnson Jul 17, 2023
0270c8d
peer review
caleb-johnson Jul 17, 2023
71d33f7
peer review
caleb-johnson Jul 17, 2023
0c60880
fix var name
caleb-johnson Jul 17, 2023
b331cff
test against dagcircuits
caleb-johnson Jul 17, 2023
8330b3b
lint
caleb-johnson Jul 18, 2023
6aa0a7d
test remove_idle_wires
caleb-johnson Jul 18, 2023
eeacdde
improve release note
caleb-johnson Jul 18, 2023
89817bc
Update separable_circuits docstring
caleb-johnson Jul 18, 2023
3696d2a
Sort the dag circuits in tests to prevent them breaking in future
caleb-johnson Jul 18, 2023
6a8611d
minor cleanup
caleb-johnson Jul 18, 2023
7cb417f
minor cleanup
caleb-johnson Jul 18, 2023
919a42e
Fix sorting
caleb-johnson Jul 18, 2023
55db180
Fix type-hint resolution
jakelishman Jul 18, 2023
e04fc8b
black
caleb-johnson Jul 18, 2023
4596431
Merge branch 'disconnected-components' of github.com:caleb-johnson/qi…
caleb-johnson Jul 18, 2023
1428b7f
Merge branch 'main' into disconnected-components
caleb-johnson Jul 18, 2023
c3dd9f1
unused import
caleb-johnson Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions test/python/dagcircuit/test_dagcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
)
from qiskit.circuit.classical import expr
from qiskit.circuit.library import IGate, HGate, CXGate, CZGate, XGate, YGate, U1Gate, RXGate
from qiskit.converters import circuit_to_dag
from qiskit.converters import circuit_to_dag, dag_to_circuit
from qiskit.test import QiskitTestCase


Expand Down Expand Up @@ -1356,6 +1356,11 @@ def test_circuit_factors(self):
"""Test number of separable factors in circuit."""
self.assertEqual(self.dag.num_tensor_factors(), 2)

def _min_active_qubit(self, d):
"""Return the minimum index of all active qubits."""
circ = dag_to_circuit(d)
return min({circ.find_bit(inst.qubits[i]).index for inst in circ for i in range(len(inst.qubits))})

Copy link
Member

@jakelishman jakelishman Jul 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is quite correct for the case of remove_idle_qubits=True, because it's going to take indices from the cut-down DAGs rather than the big dags, so the indices won't be the same. I think you'll want to get

def sort_key(indices: dict[Qubit, int]):
    def sorter(dag):
        try:
            first_op = next(dag.topological_op_nodes())
        except StopIteration:
            return -1
        return min(indices[q] for q in first_op.qargs)
    return sorter

def test_separable_circuits(self):
    big_dag = ...
    indices = {bit: i for i, bit in enumerate(big_dag)}
    components = [...]
    test = sorted(big_dag.separable_circuits(), key=sort_key(indices))
    expected = sorted(components, key=sort_key(indices))
    self.assertEqual(test, expected)

(or something like this - I just wrote that cold into GitHub)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again, this worked as-is :)

def test_separable_circuits(self):
"""Test separating disconnected sets of qubits in a circuit."""
# Empty case
Expand Down Expand Up @@ -1388,7 +1393,7 @@ def test_separable_circuits(self):

compare_dags = [comp_dag1, comp_dag2, comp_dag3]

dags = dag.separable_circuits(remove_idle_qubits=True)
dags = sorted(dag.separable_circuits(remove_idle_qubits=True), key=self._min_active_qubit)

self.assertEqual(dags, compare_dags)

Expand All @@ -1409,7 +1414,7 @@ def test_separable_circuits(self):

compare_dags = [comp_dag1, comp_dag2]

dags = dag.separable_circuits(remove_idle_qubits=True)
dags = sorted(dag.separable_circuits(remove_idle_qubits=True), key=self._min_active_qubit)

self.assertEqual(dags, compare_dags)

Expand All @@ -1428,7 +1433,7 @@ def test_separable_circuits(self):

compare_dags = [comp_dag1]

dags = dag.separable_circuits(remove_idle_qubits=True)
dags = sorted(dag.separable_circuits(remove_idle_qubits=True), key=self._min_active_qubit)

self.assertEqual(dags, compare_dags)

Expand Down Expand Up @@ -1461,7 +1466,7 @@ def test_separable_circuits_w_measurements(self):
qcs = [qc1, qc2, qc3]
compare_dags = [circuit_to_dag(qc) for qc in qcs]

dags = dag.separable_circuits()
dags = sorted(dag.separable_circuits(), key=self._min_active_qubit)

self.assertEqual(dags, compare_dags)

Expand Down
Loading