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

new unroll for-loops transpilation pass #9670

Merged
merged 24 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5158eed
dynamic circuit optimization: unroll for loops
1ucian0 Feb 26, 2023
57313e0
Merge branch 'main' of github.com:Qiskit/qiskit-terra into feature/no…
1ucian0 Feb 28, 2023
eb9b3af
exceptions
1ucian0 Feb 28, 2023
7f1e42a
check inside conditional blocks
1ucian0 Feb 28, 2023
5a4e8ce
docs
1ucian0 Feb 28, 2023
aed085e
reno
1ucian0 Feb 28, 2023
9001559
Merge branch 'main' into feature/no-ref/unroll-forloops
1ucian0 Mar 1, 2023
fd8d939
Update qiskit/transpiler/passes/optimization/unroll_forloops.py
1ucian0 Mar 2, 2023
b9f0a5f
Merge branch 'main' of github.com:Qiskit/qiskit-terra into feature/no…
1ucian0 Mar 3, 2023
4045b35
Merge branch 'main' of github.com:Qiskit/qiskit-terra into feature/no…
1ucian0 Mar 3, 2023
9422e4f
parameterless support
1ucian0 Mar 3, 2023
41312ad
moved to utils
1ucian0 Mar 3, 2023
b33e1fa
no classmethod, but function
1ucian0 Mar 3, 2023
fbbcdb7
docstring and __init__
1ucian0 Mar 3, 2023
5b13707
Update qiskit/transpiler/passes/optimization/unroll_forloops.py
1ucian0 Mar 3, 2023
925e6fc
Merge branch 'feature/no-ref/unroll-forloops' of github.com:1ucian0/q…
1ucian0 Mar 3, 2023
ea53541
Update test/python/transpiler/test_unroll_forloops.py
1ucian0 Mar 3, 2023
c1966fe
nested for-loops test
1ucian0 Mar 3, 2023
87bc0b0
Merge branch 'feature/no-ref/unroll-forloops' of github.com:1ucian0/q…
1ucian0 Mar 3, 2023
8039977
docstring note
1ucian0 Mar 3, 2023
c20f236
new test with c_if
1ucian0 Mar 3, 2023
f85ae3c
Merge branch 'main' into feature/no-ref/unroll-forloops
1ucian0 Mar 10, 2023
be0a50d
Merge branch 'main' into feature/no-ref/unroll-forloops
1ucian0 Apr 12, 2023
c445575
Remove commented-out code
jakelishman Apr 13, 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
43 changes: 43 additions & 0 deletions qiskit/transpiler/passes/optimization/unroll_forloops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""TODO"""
from qiskit.circuit import ForLoopOp
from qiskit.transpiler.basepasses import TransformationPass
from qiskit.transpiler.passes.utils import control_flow
from qiskit.converters import circuit_to_dag


class UnrollForLoops(TransformationPass):
"""TODO"""

@control_flow.trivial_recurse
def run(self, dag):
"""Run the UnrollForLoops pass on `dag`.

Args:
dag (DAGCircuit): the directed acyclic graph to run on.

Returns:
DAGCircuit: Transformed DAG.
"""
for forloop_op in dag.op_nodes(ForLoopOp):
(indexset, loop_parameter, body) = forloop_op.op.params
# TODO do not unroll when break_loop inside body

1ucian0 marked this conversation as resolved.
Show resolved Hide resolved
unrolled_dag = circuit_to_dag(body).copy_empty_like()
for index_value in indexset:
bound_body_dag = circuit_to_dag(body.bind_parameters({loop_parameter: index_value}))
unrolled_dag.compose(bound_body_dag, inplace=True)
dag.substitute_node_with_dag(forloop_op, unrolled_dag)

return dag
51 changes: 51 additions & 0 deletions test/python/transpiler/test_unroll_forloops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This code is part of Qiskit.
#
# (C) Copyright IBM 2023.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Test the unroll_forloops pass"""

import unittest

from qiskit.circuit import QuantumCircuit, Parameter, QuantumRegister, ClassicalRegister
from qiskit.transpiler import PassManager
from qiskit.test import QiskitTestCase
from qiskit.transpiler.passes.optimization.unroll_forloops import UnrollForLoops


class TestUnrool(QiskitTestCase):
1ucian0 marked this conversation as resolved.
Show resolved Hide resolved
"""Test UnrollForLoops pass"""

def test_range(self):
"""TODO"""
qreg, creg = QuantumRegister(5, "q"), ClassicalRegister(2, "c")

body = QuantumCircuit(3, 1)
loop_parameter = Parameter("foo")
indexset = range(0, 10, 2)

body.rx(loop_parameter, [0, 1, 2])

circuit = QuantumCircuit(qreg, creg)
circuit.for_loop(indexset, loop_parameter, body, [1, 2, 3], [1])

expected = QuantumCircuit(qreg, creg)
for index_loop in indexset:
expected.rx(index_loop, [1, 2, 3])

passmanager = PassManager()
passmanager.append(UnrollForLoops())
result = passmanager.run(circuit)

self.assertEqual(result, expected)


if __name__ == "__main__":
unittest.main()