Skip to content

Commit

Permalink
Add and change some docstrings for a better quality (#174)
Browse files Browse the repository at this point in the history
* Small change.

* First check.

* Typo.

* Add link.

* Change.

* Change.

* Update pennylane_qiskit/qiskit_device.py

* Update from review.

* Typo

* Update docstring transpiler.

* Update.

Co-authored-by: antalszava <[email protected]>
  • Loading branch information
rmoyard and antalszava authored Jan 29, 2022
1 parent 66e2185 commit 6385f01
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pennylane_qiskit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from ._version import __version__
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
from .runtime_devices import IBMQCircuitRunnerDevice
from .runtime_devices import IBMQSamplerDevice
from .vqe_runtime_runner import vqe_runner, upload_vqe_runner, delete_vqe_runner
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.
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 transpiler documentation <https://qiskit.org/documentation/stubs/qiskit.compiler.transpile.html>`_
"""
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 @@ -290,7 +298,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): operation to be checked
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 @@ -312,7 +328,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.
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 @@ -324,7 +344,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 or None): the name of the experiment to get the state for.
Returns:
array[float]: size ``(2**num_wires,)`` statevector
Expand All @@ -349,7 +369,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 or None): the name of the circuit to get the state for
Returns:
array[complex]: array of samples in the shape ``(dev.shots, dev.num_wires)``
Expand Down

0 comments on commit 6385f01

Please sign in to comment.