Skip to content

Commit

Permalink
[WIP] Pass transpilation options (#108)
Browse files Browse the repository at this point in the history
* WIP Pass transpilation options

* WIP Pass transpilation options

* WIP Pass transpilation options

* WIP Pass transpilation options

* WIP PassTranspilationOptions

* WIP PassTranspilationOptions

* WIP PassTranspilationOptions

* WIP PassTranspilationOptions

* WIP PassTranspilationOptions

* WIP PassTranspilationOptions

* Apply suggestions from code review

Typos corrected.

Co-authored-by: antalszava <[email protected]>

* API Doc changes

* CHANGELOG.md updated. Documentation sugessions incorporated.

Co-authored-by: antalszava <[email protected]>
  • Loading branch information
sagarpahwa and antalszava authored Oct 24, 2020
1 parent 28064a9 commit 7ba24ac
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ This release contains contributions from (in alphabetical order):

### Improvements

* Qiskit devices are now allowed to pass transpilation options.
[(#108)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/108)

* The provided devices are now compatible with Qiskit 0.23.
[(#112)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/112)

Expand All @@ -35,7 +38,7 @@ This release contains contributions from (in alphabetical order):

This release contains contributions from (in alphabetical order):

Josh Izaac, Nathan Killoran, Antal Száva
Josh Izaac, Nathan Killoran, Sagar Pahwa, Antal Száva

---

Expand Down
5 changes: 3 additions & 2 deletions pennylane_qiskit/aer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class AerDevice(QiskitDevice):
"""A PennyLane device for the C++ Qiskit Aer simulator.
Please refer to the `Qiskit documentation <https://qiskit.org/documentation/>`_ for
for further information to the noise model and backend options.
further information on the noise model, backend options and transpile options.
A range of :code:`backend_options` can be given as kwargs that will be passed to the simulator.
A range of :code:`backend_options` that will be passed to the simulator and
a range of transpile options can be given as kwargs.
For more information on backends, please visit the
`Aer provider documentation <https://qiskit.org/documentation/apidoc/aer_provider.html>`_.
Expand Down
5 changes: 3 additions & 2 deletions pennylane_qiskit/basic_aer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ class BasicAerDevice(QiskitDevice):
"""A PennyLane device for the native Python Qiskit simulator.
Please see the `Qiskit documentations <https://qiskit.org/documentation/>`_
for more details.
further information on the backend options and transpile options.
A range of :code:`backend_options` can be given in as kwargs that will be passed to the simulator.
A range of :code:`backend_options` that will be passed to the simulator and
a range of transpile options can be given as kwargs.
For more information on backends, please visit the
`Basic Aer provider documentation <https://qiskit.org/documentation/apidoc/providers_basicaer.html>`_.
Expand Down
22 changes: 17 additions & 5 deletions pennylane_qiskit/qiskit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ def __init__(self, wires, provider, backend, shots=1024, **kwargs):

self.noise_model = kwargs.pop("noise_model")

# set transpile_args
self.set_transpile_args(**kwargs)

# Get further arguments for run
s = inspect.signature(b.run)
self.run_args = {}
Expand All @@ -194,9 +197,16 @@ def __init__(self, wires, provider, backend, shots=1024, **kwargs):
# BasicAer
self.run_args["backend_options"] = kwargs

def set_transpile_args(self, **kwargs):
"""The transpile argument setter."""
transpile_sig = inspect.signature(transpile).parameters
self.transpile_args = {arg: kwargs[arg] for arg in transpile_sig if arg in kwargs}
self.transpile_args.pop("circuits", None)
self.transpile_args.pop("backend", None)

@property
def backend(self):
"""The Qiskit simulation backend object"""
"""The Qiskit simulation backend object."""
return self.provider.get_backend(self.backend_name)

def reset(self):
Expand Down Expand Up @@ -296,11 +306,13 @@ def qubit_unitary_check(operation, par, wires):
)

def compile(self):
"""Compile the quantum circuit to target
the provided compile_backend. If compile_backend is None,
then the target is simply the backend."""
"""Compile the quantum circuit to target the provided compile_backend.
If compile_backend is None, then the target is simply the
backend.
"""
compile_backend = self.compile_backend or self.backend
compiled_circuits = transpile(self._circuit, backend=compile_backend)
compiled_circuits = transpile(self._circuit, backend=compile_backend, **self.transpile_args)

# Specify to have a memory for hw/hw simulators
memory = str(compile_backend) not in self._state_backends
Expand Down
6 changes: 4 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ def device(request, backend, shots, analytic):
if backend not in state_backends and analytic == True:
pytest.skip("Hardware simulators do not support analytic mode")

def _device(n):
return request.param(wires=n, backend=backend, shots=shots, analytic=analytic)
def _device(n, device_options=None):
if device_options is None:
device_options = {}
return request.param(wires=n, backend=backend, shots=shots, analytic=analytic, **device_options)

return _device

Expand Down
32 changes: 32 additions & 0 deletions tests/test_qiskit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
import pennylane as qml
from pennylane_qiskit import AerDevice

test_transpile_options = [
{},
{'optimization_level':2},
{'optimization_level':2, 'seed_transpiler':22}
]

test_device_options = [
{},
{'optimization_level':3},
{'optimization_level':1}
]

class TestProbabilities:
"""Tests for the probability function"""
Expand All @@ -14,6 +25,27 @@ def test_probability_no_results(self):
dev = AerDevice(backend="statevector_simulator", wires=1, analytic=True)
assert dev.probability() is None

@pytest.mark.parametrize("analytic", [True])
@pytest.mark.parametrize("wires", [1,2,3])
@pytest.mark.parametrize("shots", [2])
@pytest.mark.parametrize("device_options", test_device_options)
@pytest.mark.transpile_args_test
class TestTranspilationOptionInitialization:
"""Tests for passing the transpilation options to qiskit at time of device
initialization."""

def test_device_with_transpilation_options(self, device, wires, device_options):
"""Test that the transpilation options must be persisted if provided."""
dev = device(wires, device_options)
assert dev.transpile_args == device_options

@pytest.mark.parametrize("transpile_options", test_transpile_options)
def test_transpilation_option_update(self, device, wires, device_options, transpile_options):
"""Test that the transpilation options are updated as expected."""
dev = device(wires, device_options)
assert dev.transpile_args == device_options
dev.set_transpile_args(**transpile_options)
assert dev.transpile_args == transpile_options

class TestAnalyticWarningHWSimulator:
"""Tests the warnings for when the analytic attribute of a device is set to true"""
Expand Down

0 comments on commit 7ba24ac

Please sign in to comment.