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 and change some docstrings for a better quality #174

Merged
merged 19 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 2 additions & 1 deletion pennylane_qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"""Top level PennyLane-qiskit module"""

from ._version import __version__
from .qiskit_device import QiskitDevice
rmoyard marked this conversation as resolved.
Show resolved Hide resolved
from .aer import AerDevice
from .basic_aer import BasicAerDevice
from .converter import load, load_qasm, load_qasm_from_file
from .ibmq import IBMQDevice
from .converter import load, load_qasm, load_qasm_from_file
3 changes: 3 additions & 0 deletions pennylane_qiskit/ibmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ class IBMQDevice(QiskitDevice):
variable ``IBMQX_URL`` is used, followed by the default URL.
noise_model (NoiseModel): NoiseModel Object from ``qiskit.providers.aer.noise``.
Only applicable for simulator backends.
hub (str): Name of the provider hub.
rmoyard marked this conversation as resolved.
Show resolved Hide resolved
group (str): Name of the provider group.
project (str): Name of the provider project.
"""

short_name = "qiskit.ibmq"
Expand Down
44 changes: 32 additions & 12 deletions pennylane_qiskit/qiskit_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
"T": ex.TGate,
"SX": ex.SXGate,
"Identity": ex.IGate,
# Adding the following for conversion compatibility
"CSWAP": ex.CSwapGate,
"CRX": ex.CRXGate,
"CRY": ex.CRYGate,
Expand All @@ -64,7 +63,7 @@
}

# Separate dictionary for the inverses as the operations dictionary needs
# to be invertable for the conversion functionality to work
# to be invertible for the conversion functionality to work
QISKIT_OPERATION_INVERSES_MAP = {k + ".inv": v for k, v in QISKIT_OPERATION_MAP.items()}


Expand All @@ -78,7 +77,7 @@ class QiskitDevice(QubitDevice, abc.ABC):
provider (Provider): The Qiskit simulation provider
backend (str): the desired backend
shots (int or None): number of circuit evaluations/random samples used
to estimate expectation values and variances of observables. For statevector backends,
to estimate expectation values and variances of observables. For state vector backends,
setting to ``None`` results in computing statistics like expectation values and variances analytically.

Keyword Args:
Expand Down Expand Up @@ -131,14 +130,14 @@ def __init__(self, wires, provider, backend, shots=1024, **kwargs):
self.backend_name = backend
self._capabilities["backend"] = [b.name() for b in self.provider.backends()]

# check that the backend exists
# Check that the backend exists
if backend not in self._capabilities["backend"]:
raise ValueError(
f"Backend '{backend}' does not exist. Available backends "
f"are:\n {self._capabilities['backend']}"
)

# perform validation against backend
# Perform validation against backend
b = self.backend
if len(self.wires) > b.configuration().n_qubits:
raise ValueError(
Expand All @@ -151,7 +150,7 @@ def __init__(self, wires, provider, backend, shots=1024, **kwargs):
self.process_kwargs(kwargs)

def process_kwargs(self, kwargs):
"""Processing the keyword arguments that were provided upon device creation.
"""Processing the keyword arguments that were provided upon device initialization.

Args:
kwargs (dict): keyword arguments to be set for the device
Expand Down Expand Up @@ -181,15 +180,24 @@ def process_kwargs(self, kwargs):
self.run_args.update(kwargs)

def set_transpile_args(self, **kwargs):
"""The transpile argument setter."""
"""The transpile argument setter.

Keyword Args:
kwargs (dict): keyword arguments to be set for the Qiskit transpiler. For more details,
see the `Qiskit documentation <https://qiskit.org/documentation/stubs/qiskit.compiler.transpile.html>`_.
Copy link
Contributor

Choose a reason for hiding this comment

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

As with the example in pennylane_qiskit/ibmq.py, instead of a general item, we'd want some specific set of keyword arguments here, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@antalszava Yes that would be ideal but they are so many transpiler arguments that I thought it was better just to link it, what do you think?

"""
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.

Returns:
qiskit.providers.backend: Qiskit backend object.
"""
if self._backend is None:
self._backend = self.provider.get_backend(self.backend_name)
return self._backend
Expand Down Expand Up @@ -291,7 +299,15 @@ def apply_operations(self, operations):
return circuits

def qubit_state_vector_check(self, operation, par, wires):
"""Input check for the the QubitStateVector operation."""
"""Input check for the the QubitStateVector operation.

Args:
operation (pennylane.Operation): operations to be checked
rmoyard marked this conversation as resolved.
Show resolved Hide resolved

Raises:
DeviceError: If the operation is QubitStateVector
ValueError: If the state has not the right length
"""
if operation == "QubitStateVector":
if "unitary" in self.backend_name:
raise DeviceError(
Expand All @@ -313,7 +329,11 @@ def compile(self):
return compiled_circuits

def run(self, qcirc):
"""Run the compiled circuit, and query the result."""
"""Run the compiled circuit, and query the result.
rmoyard marked this conversation as resolved.
Show resolved Hide resolved

Args:
qcirc (qiskit.QuantumCircuit): the quantum circuit to be run on the backend
"""
self._current_job = self.backend.run(qcirc, shots=self.shots, **self.run_args)
result = self._current_job.result()

Expand All @@ -325,7 +345,7 @@ def _get_state(self, result, experiment=None):

Args:
result (qiskit.Result): result object
experiment (str): the name of the experiment to get the state for
experiment (str): Optional, the name of the experiment to get the state for. Default=None.
rmoyard marked this conversation as resolved.
Show resolved Hide resolved

Returns:
array[float]: size ``(2**num_wires,)`` statevector
Expand All @@ -350,7 +370,7 @@ def generate_samples(self, circuit=None):
:math:`q_0` is the most significant bit.

Args:
circuit (str): the name of the circuit to get the state for
circuit (str): Optional, the name of the circuit to get the state for
rmoyard marked this conversation as resolved.
Show resolved Hide resolved

Returns:
array[complex]: array of samples in the shape ``(dev.shots, dev.num_wires)``
Expand Down