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

transpile a list of circuits with a single element should return a list with a single element #5298

Merged
merged 3 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 11 additions & 11 deletions qiskit/compiler/transpile.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ def callback_func(**kwargs):
TranspilerError: in case of bad inputs to transpiler (like conflicting parameters)
or errors in passes
"""
circuits = circuits if isinstance(circuits, list) else [circuits]
arg_circuits_list = isinstance(circuits, list)
circuits = circuits if arg_circuits_list else [circuits]

# transpiling schedules is not supported yet.
start_time = time()
if all(isinstance(c, Schedule) for c in circuits):
warnings.warn("Transpiling schedules is not supported yet.", UserWarning)
if len(circuits) == 1:
end_time = time()
_log_transpile_time(start_time, end_time)
return circuits[0]
end_time = time()
_log_transpile_time(start_time, end_time)
return circuits
if arg_circuits_list:
return circuits
else:
return circuits[0]

if pass_manager is not None:
_check_conflicting_argument(optimization_level=optimization_level, basis_gates=basis_gates,
Expand Down Expand Up @@ -242,13 +242,13 @@ def callback_func(**kwargs):
# Transpile circuits in parallel
circuits = parallel_map(_transpile_circuit, list(zip(circuits, transpile_args)))

if len(circuits) == 1:
end_time = time()
_log_transpile_time(start_time, end_time)
return circuits[0]
end_time = time()
_log_transpile_time(start_time, end_time)
return circuits

if arg_circuits_list:
return circuits
else:
return circuits[0]


def _check_conflicting_argument(**kargs):
Expand Down
23 changes: 23 additions & 0 deletions releasenotes/notes/transpile_singleton-61755c5cf1970111.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
fixes:
- |
When running :func:`qiskit.compiler.transpile` on a list of circuits with a
single element, the function used to return a circuit instead of a list. Now,
when :func:`qiskit.compiler.transpile` is called with a list, it will return a
list if when that list has a single element. See
`#5260 <https://github.com/Qiskit/qiskit-terra/issues/5260>`__.

.. code-block:: python

from qiskit import *

qc = QuantumCircuit(2)
qc.h(0)
qc.cx(0, 1)
qc.measure_all()

transpiled = transpile([qc])
print(type(transpiled), len(transpiled))

.. parsed-literal::
<class 'list'> 1
16 changes: 16 additions & 0 deletions test/python/compiler/test_transpiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,22 @@ def test_transpile_two(self):
self.assertIsInstance(circuits[0], QuantumCircuit)
self.assertIsInstance(circuits[1], QuantumCircuit)

def test_transpile_singleton(self):
"""Test transpile a single-element list with a circuit.
See https://github.com/Qiskit/qiskit-terra/issues/5260"""
backend = BasicAer.get_backend('qasm_simulator')

qubit_reg = QuantumRegister(2)
clbit_reg = ClassicalRegister(2)
qc = QuantumCircuit(qubit_reg, clbit_reg, name="bell")
qc.h(qubit_reg[0])
qc.cx(qubit_reg[0], qubit_reg[1])
qc.measure(qubit_reg, clbit_reg)

circuits = transpile([qc], backend)
self.assertEqual(len(circuits), 1)
self.assertIsInstance(circuits[0], QuantumCircuit)

def test_mapping_correction(self):
"""Test mapping works in previous failed case.
"""
Expand Down