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

Deprecate execute() #11044

Merged
merged 17 commits into from
Jan 13, 2024
Merged
8 changes: 8 additions & 0 deletions qiskit/execute_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from qiskit.providers.backend import Backend
from qiskit.pulse import Schedule, ScheduleBlock
from qiskit.exceptions import QiskitError
from qiskit.utils.deprecation import deprecate_func

logger = logging.getLogger(__name__)

Expand All @@ -35,6 +36,13 @@ def _log_submission_time(start_time, end_time):
logger.info(log_msg)


@deprecate_func(
additional_msg="This function combines ``transpile`` and ``backend.run``, which is covered "
"by ``Sampler`` :mod:`~qiskit.primitives`. Alternatively, you can also run :func:`.transpile` "
"followed by ``backend.run()``. The ``execute`` function is going to be removed "
"in 1.0.",
since="0.46.0",
)
def execute(
experiments,
backend,
Expand Down
4 changes: 2 additions & 2 deletions qiskit/test/providers/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from unittest import SkipTest

from qiskit import execute
from qiskit import transpile
from ..base import QiskitTestCase
from ..reference_circuits import ReferenceCircuits

Expand Down Expand Up @@ -69,7 +69,7 @@ def test_status(self):

def test_run_circuit(self):
"""Test running a single circuit."""
job = execute(self.circuit, self.backend)
job = self.backend.run(transpile(self.circuit, self.backend))
result = job.result()
self.assertEqual(result.success, True)
return result
30 changes: 30 additions & 0 deletions releasenotes/notes/deprecate-execute-67a5c68b93722057.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
deprecations:
- |
Qiskit's :func:`~.execute_function.execute` function is deprecated.
This function served as a high-level wrapper around transpiling
a circuit with some transpile options and running it on a backend
with some run options. To do the same thing, you can explicitly
Copy link
Member

Choose a reason for hiding this comment

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

Do you want to call out the sampler here too? The BackendSampler class is fairly straightforward mapping to what execute() does (although imo execute() or transpile() followed by backend.run() are much easier to work with).

Copy link
Member Author

Choose a reason for hiding this comment

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

I added an example in 0367d40

use the :func:`~.transpile` function (with appropriate transpile
options) followed by ``backend.run()`` (with appropriate run options).

For example, instead of running::

from qiskit import execute
job = execute(circuit, backend)

you can run::

from qiskit import transpile
new_circuit = transpile(circuit, backend)
job = backend.run(new_circuit)

Alternatively, the ``Sampler`` primitive is semantically equivalent to the
deprecated :func:`~.execute_function.execute` function. The class
:class:`.BackendSampler` is a generic wrapper for backends that do not support
primitives:

from qiskit.primitives import BackendSampler
sampler = BackendSampler(backend)
job = sampler.run(circuit)

6 changes: 3 additions & 3 deletions test/python/algorithms/test_measure_error_mitigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,9 @@ def test_tensor_subset_fitter(self):
circuit.measure(2, 1)
circuit.measure(0, 2)

result = execute(
circuit, backend, noise_model=noise_model, shots=1000, seed_simulator=0
).result()
with self.assertWarns(DeprecationWarning):
job = execute(circuit, backend, noise_model=noise_model, shots=1000, seed_simulator=0)
result = job.result()
with self.subTest(subset=subset):
with self.assertWarns(DeprecationWarning):
new_result = fitter.subset_fitter([1, 2, 0]).filter.apply(result)
Expand Down
9 changes: 4 additions & 5 deletions test/python/basicaer/test_basicaer_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

from qiskit import BasicAer
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import execute
from qiskit.result import Result
from qiskit.providers.basicaer import BasicAerError
from qiskit.test import QiskitTestCase
Expand All @@ -33,7 +32,7 @@ def setUp(self):
self._qc2 = QuantumCircuit(qr, cr, name="qc2")
self._qc1.measure(qr[0], cr[0])
self.backend = BasicAer.get_backend("qasm_simulator")
self._result1 = execute(self._qc1, self.backend).result()
self._result1 = self.backend.run(self._qc1).result()
Comment on lines -36 to +35
Copy link
Member

Choose a reason for hiding this comment

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

Not that I imagine it will matter here, but technically this is a behavioural change due to the missing transpile. Same comment for most changes in the next two files.


def test_builtin_simulator_result_fields(self):
"""Test components of a result from a local simulator."""
Expand All @@ -52,7 +51,7 @@ def test_basicaer_execute(self):
qc.cx(qubit_reg[0], qubit_reg[1])
qc.measure(qubit_reg, clbit_reg)

job = execute(qc, self.backend)
job = self.backend.run(qc)
result = job.result()
self.assertIsInstance(result, Result)

Expand All @@ -66,7 +65,7 @@ def test_basicaer_execute_two(self):
qc.measure(qubit_reg, clbit_reg)
qc_extra = QuantumCircuit(qubit_reg, clbit_reg, name="extra")
qc_extra.measure(qubit_reg, clbit_reg)
job = execute([qc, qc_extra], self.backend)
job = self.backend.run([qc, qc_extra])
result = job.result()
self.assertIsInstance(result, Result)

Expand All @@ -76,7 +75,7 @@ def test_basicaer_num_qubits(self):
qc.x(0)
qc.measure(0, 0)
with self.assertRaises(BasicAerError):
execute(qc, self.backend)
self.backend.run(qc)


if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions test/python/basicaer/test_multi_registers_convention.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

"""Test executing multiple-register circuits on BasicAer."""

from qiskit import BasicAer, execute
from qiskit import BasicAer
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister
from qiskit.quantum_info import Operator, Statevector, process_fidelity, state_fidelity
from qiskit.test import QiskitTestCase
Expand All @@ -39,17 +39,17 @@ def test_circuit_multi(self):

backend_sim = BasicAer.get_backend("qasm_simulator")

result = execute(qc, backend_sim, seed_transpiler=34342).result()
result = backend_sim.run(qc, seed_transpiler=34342).result()
counts = result.get_counts(qc)

target = {"01 10": 1024}

backend_sim = BasicAer.get_backend("statevector_simulator")
result = execute(circ, backend_sim, seed_transpiler=3438).result()
result = backend_sim.run(circ, seed_transpiler=3438).result()
state = result.get_statevector(circ)

backend_sim = BasicAer.get_backend("unitary_simulator")
result = execute(circ, backend_sim, seed_transpiler=3438).result()
result = backend_sim.run(circ, seed_transpiler=3438).result()
jakelishman marked this conversation as resolved.
Show resolved Hide resolved
unitary = Operator(result.get_unitary(circ))

self.assertEqual(counts, target)
Expand Down
22 changes: 12 additions & 10 deletions test/python/basicaer/test_qasm_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def assertExecuteLog(self, log_msg):
qr = QuantumRegister(2, "qr")
cr = ClassicalRegister(4, "cr")
circuit = QuantumCircuit(qr, cr)
execute(circuit, backend=self.backend, shots=shots, seed_simulator=self.seed)
with self.assertWarns(DeprecationWarning):
execute(circuit, backend=self.backend, shots=shots, seed_simulator=self.seed)
self.log_output.seek(0)
# Filter unrelated log lines
output_lines = self.log_output.readlines()
Expand Down Expand Up @@ -92,7 +93,7 @@ def test_measure_sampler_repeated_qubits(self):
circuit.measure(qr[1], cr[2])
circuit.measure(qr[0], cr[3])
target = {"0110": shots}
job = execute(circuit, backend=self.backend, shots=shots, seed_simulator=self.seed)
job = self.backend.run(circuit, shots=shots, seed_simulator=self.seed)
result = job.result()
counts = result.get_counts(0)
self.assertEqual(counts, target)
Expand All @@ -109,7 +110,7 @@ def test_measure_sampler_single_qubit(self):
circuit.x(qr[qubit])
circuit.measure(qr[qubit], cr[0])
target = {"1": shots}
job = execute(circuit, backend=self.backend, shots=shots, seed_simulator=self.seed)
job = self.backend.run(circuit, shots=shots, seed_simulator=self.seed)
result = job.result()
counts = result.get_counts(0)
self.assertEqual(counts, target)
Expand Down Expand Up @@ -146,7 +147,7 @@ def test_measure_sampler_partial_qubit(self):
circuit.barrier(qr)
circuit.measure(qr[3], cr[3])
target = {"1011": shots}
job = execute(circuit, backend=self.backend, shots=shots, seed_simulator=self.seed)
job = self.backend.run(circuit, shots=shots, seed_simulator=self.seed)
result = job.result()
counts = result.get_counts(0)
self.assertEqual(counts, target)
Expand Down Expand Up @@ -215,9 +216,8 @@ def test_if_statement(self):
circuit_if_false.measure(qr[0], cr[0])
circuit_if_false.measure(qr[1], cr[1])
circuit_if_false.measure(qr[2], cr[2])
job = execute(
job = self.backend.run(
[circuit_if_true, circuit_if_false],
backend=self.backend,
shots=shots,
seed_simulator=self.seed,
)
Expand Down Expand Up @@ -252,7 +252,7 @@ def test_bit_cif_crossaffect(self):
circuit.measure(qr[2], cr[2])
circuit.h(qr[0]).c_if(cr[0], True)
circuit.measure(qr[0], cr1[0])
job = execute(circuit, backend=self.backend, shots=shots, seed_simulator=self.seed)
job = self.backend.run(circuit, shots=shots, seed_simulator=self.seed)
result = job.result().get_counts()
target = {"0 110": 100}
self.assertEqual(result, target)
Expand Down Expand Up @@ -292,7 +292,9 @@ def test_teleport(self):
circuit.z(qr[2]).c_if(cr0, 1)
circuit.x(qr[2]).c_if(cr1, 1)
circuit.measure(qr[2], cr2[0])
job = execute(circuit, backend=self.backend, shots=shots, seed_simulator=self.seed)
job = self.backend.run(
transpile(circuit, self.backend), shots=shots, seed_simulator=self.seed
)
results = job.result()
data = results.get_counts("teleport")
alice = {
Expand Down Expand Up @@ -345,7 +347,7 @@ def test_memory(self):
circ.measure(qr[3], cr1[1])

shots = 50
job = execute(circ, backend=self.backend, shots=shots, memory=True)
job = self.backend.run(circ, shots=shots, memory=True)
result = job.result()
memory = result.get_memory()
self.assertEqual(len(memory), shots)
Expand All @@ -372,7 +374,7 @@ def test_unitary(self):
circuit = QuantumCircuit(qr, cr)
circuit.unitary(multi_x, qr)
circuit.measure(qr, cr)
job = execute(circuit, self.backend, shots=shots)
job = self.backend.run(transpile(circuit), shots=shots)
result = job.result()
counts = result.get_counts(0)
self.assertEqual(counts, target_counts)
Expand Down
4 changes: 2 additions & 2 deletions test/python/basicaer/test_statevector_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from qiskit.providers.basicaer import StatevectorSimulatorPy
from qiskit.test import ReferenceCircuits
from qiskit.test import providers
from qiskit import QuantumRegister, QuantumCircuit, execute
from qiskit import QuantumRegister, QuantumCircuit, transpile
from qiskit.quantum_info.random import random_unitary
from qiskit.quantum_info import state_fidelity

Expand Down Expand Up @@ -76,7 +76,7 @@ def test_unitary(self):
# Simulate output on circuit
circuit = QuantumCircuit(qr)
circuit.unitary(unitary, qr)
job = execute(circuit, self.backend)
job = self.backend.run(transpile(circuit, self.backend))
result = job.result()
psi_out = result.get_statevector(0)
fidelity = state_fidelity(psi_target, psi_out)
Expand Down
8 changes: 4 additions & 4 deletions test/python/basicaer/test_unitary_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import numpy as np

from qiskit import execute
from qiskit import transpile
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister
from qiskit.providers.basicaer import UnitarySimulatorPy
from qiskit.quantum_info.operators.predicates import matrix_equal
Expand All @@ -35,7 +35,7 @@ class BasicAerUnitarySimulatorPyTest(providers.BackendTestCase):
def test_basicaer_unitary_simulator_py(self):
"""Test unitary simulator."""
circuits = self._test_circuits()
job = execute(circuits, backend=self.backend)
job = self.backend.run(transpile(circuits, self.backend))
sim_unitaries = [job.result().get_unitary(circ) for circ in circuits]
reference_unitaries = self._reference_unitaries()
for u_sim, u_ref in zip(sim_unitaries, reference_unitaries):
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_unitary(self):
# Simulate output on circuit
circuit = QuantumCircuit(qr)
circuit.unitary(unitary, qr)
job = execute(circuit, self.backend)
job = self.backend.run(transpile(circuit, self.backend))
result = job.result()
unitary_out = Operator(result.get_unitary(0))
fidelity = process_fidelity(unitary_target, unitary_out)
Expand All @@ -124,7 +124,7 @@ def test_global_phase(self):
circuit.z(q[0])
circuit.x(q[0])

job = execute(circuit, self.backend)
job = self.backend.run(transpile(circuit, self.backend))
result = job.result()

unitary_out = result.get_unitary(circuit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ddt import ddt, data, unpack

from qiskit.test.base import QiskitTestCase
from qiskit import BasicAer, execute
from qiskit import BasicAer, transpile
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import (
LinearPauliRotations,
Expand All @@ -40,7 +40,7 @@ def assertFunctionIsCorrect(self, function_circuit, reference):
circuit.append(function_circuit.to_instruction(), list(range(circuit.num_qubits)))

backend = BasicAer.get_backend("statevector_simulator")
statevector = execute(circuit, backend).result().get_statevector()
statevector = backend.run(transpile(circuit, backend)).result().get_statevector()

probabilities = defaultdict(float)
for i, statevector_amplitude in enumerate(statevector):
Expand Down
4 changes: 2 additions & 2 deletions test/python/circuit/library/test_integer_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ddt import ddt, data, unpack

from qiskit.test.base import QiskitTestCase
from qiskit import BasicAer, execute
from qiskit import BasicAer, transpile
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import IntegerComparator

Expand All @@ -34,7 +34,7 @@ def assertComparisonIsCorrect(self, comp, num_state_qubits, value, geq):

# run simulation
backend = BasicAer.get_backend("statevector_simulator")
statevector = execute(qc, backend).result().get_statevector()
statevector = backend.run(transpile(qc, backend)).result().get_statevector()
for i, amplitude in enumerate(statevector):
prob = np.abs(amplitude) ** 2
if prob > 1e-6:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ddt import ddt, data, unpack
import numpy as np

from qiskit import BasicAer, execute
from qiskit import BasicAer, transpile
from qiskit.test.base import QiskitTestCase
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import LinearAmplitudeFunction
Expand All @@ -38,7 +38,7 @@ def assertFunctionIsCorrect(self, function_circuit, reference):
circuit.append(function_circuit.to_instruction(), list(range(circuit.num_qubits)))

backend = BasicAer.get_backend("statevector_simulator")
statevector = execute(circuit, backend).result().get_statevector()
statevector = backend.run(transpile(circuit, backend)).result().get_statevector()

probabilities = defaultdict(float)
for i, statevector_amplitude in enumerate(statevector):
Expand Down
7 changes: 1 addition & 6 deletions test/python/circuit/library/test_phase_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import numpy as np

from qiskit.test.base import QiskitTestCase
from qiskit import BasicAer, execute
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library import PhaseEstimation, QFT
from qiskit.quantum_info import Statevector
Expand Down Expand Up @@ -64,11 +63,7 @@ def assertPhaseEstimationIsCorrect(
inplace=True,
)
circuit.compose(pec, inplace=True)
# TODO use Statevector for simulation once Qiskit/qiskit-terra#4681 is resolved
# actual = Statevector.from_instruction(circuit).data
backend = BasicAer.get_backend("statevector_simulator")
actual = execute(circuit, backend).result().get_statevector()

actual = Statevector.from_instruction(circuit).data
np.testing.assert_almost_equal(reference, actual)

def test_phase_estimation(self):
Expand Down
4 changes: 2 additions & 2 deletions test/python/circuit/library/test_piecewise_chebyshev.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from ddt import ddt, data, unpack

from qiskit.test.base import QiskitTestCase
from qiskit import BasicAer, execute
from qiskit import BasicAer, transpile
from qiskit.circuit import QuantumCircuit
from qiskit.circuit.library.arithmetic.piecewise_chebyshev import PiecewiseChebyshev

Expand All @@ -37,7 +37,7 @@ def assertFunctionIsCorrect(self, function_circuit, reference):
circuit.append(function_circuit.to_instruction(), list(range(circuit.num_qubits)))

backend = BasicAer.get_backend("statevector_simulator")
statevector = execute(circuit, backend).result().get_statevector()
statevector = backend.run(transpile(circuit, backend)).result().get_statevector()

probabilities = defaultdict(float)
for i, statevector_amplitude in enumerate(statevector):
Expand Down
Loading