API documentation

Contents

API documentation#

class pytket.extensions.qsharp.QsharpSimulatorBackend(backend_name: str = 'Qsharp Backend')#

Backend for simulating a circuit using the QDK.

__init__(backend_name: str = 'Qsharp Backend')#
classmethod available_devices(**kwargs: Any) List[BackendInfo]#

Retrieve all available devices as a list of BackendInfo objects, including device name, architecture, supported gate set, gate errors, and other hardware-specific information.

Returns:

A list of BackendInfo objects describing available devices.

Return type:

List[BackendInfo]

cancel(handle: ResultHandle) None#

Cancel a job.

Parameters:

handle (ResultHandle) – handle to job

Raises:

NotImplementedError – If backend does not support job cancellation

circuit_status(handle: ResultHandle) CircuitStatus#

Return a CircuitStatus reporting the status of the circuit execution corresponding to the ResultHandle

default_compilation_pass(optimisation_level: int = 2) BasePass#

A suggested compilation pass that will will, if possible, produce an equivalent circuit suitable for running on this backend.

At a minimum it will ensure that compatible gates are used and that all two- qubit interactions are compatible with the backend’s qubit architecture. At higher optimisation levels, further optimisations may be applied.

This is a an abstract method which is implemented in the backend itself, and so is tailored to the backend’s requirements.

Parameters:

optimisation_level (int, optional) –

The level of optimisation to perform during compilation.

  • Level 0 does the minimum required to solves the device constraints, without any optimisation.

  • Level 1 additionally performs some light optimisations.

  • Level 2 (the default) adds more computationally intensive optimisations that should give the best results from execution.

Returns:

Compilation pass guaranteeing required predicates.

Return type:

BasePass

empty_cache() None#

Manually empty the result cache on the backend.

get_compiled_circuit(circuit: Circuit, optimisation_level: int = 2) Circuit#

Return a single circuit compiled with default_compilation_pass(). See Backend.get_compiled_circuits().

get_compiled_circuits(circuits: Sequence[Circuit], optimisation_level: int = 2) List[Circuit]#

Compile a sequence of circuits with default_compilation_pass() and return the list of compiled circuits (does not act in place).

As well as applying a degree of optimisation (controlled by the optimisation_level parameter), this method tries to ensure that the circuits can be run on the backend (i.e. successfully passed to process_circuits()), for example by rebasing to the supported gate set, or routing to match the connectivity of the device. However, this is not always possible, for example if the circuit contains classical operations that are not supported by the backend. You may use valid_circuit() to check whether the circuit meets the backend’s requirements after compilation. This validity check is included in process_circuits() by default, before any circuits are submitted to the backend.

If the validity check fails, you can obtain more information about the failure by iterating through the predicates in the required_predicates property of the backend, and running the verify() method on each in turn with your circuit.

Parameters:
  • circuits – The circuits to compile.

  • optimisation_level (int, optional) – The level of optimisation to perform during compilation. See default_compilation_pass() for a description of the different levels (0, 1 or 2). Defaults to 2.

Returns:

Compiled circuits.

Return type:

List[Circuit]

get_result(handle: ResultHandle, **kwargs: int | float | str | None) BackendResult#

Return a BackendResult corresponding to the handle.

Use keyword arguments to specify parameters to be used in retrieving results. See specific Backend derived class for available parameters, from the following list:

  • timeout: maximum time to wait for remote job to finish

  • wait: polling interval between remote calls to check job status

Parameters:

handle (ResultHandle) – handle to results

Returns:

Results corresponding to handle.

Return type:

BackendResult

get_results(handles: Iterable[ResultHandle], **kwargs: int | float | str | None) List[BackendResult]#

Return results corresponding to handles.

Parameters:

handles – Iterable of handles

Returns:

List of results

Keyword arguments are as for get_result, and apply to all jobs.

pop_result(handle: ResultHandle) Dict[str, Any] | None#

Remove cache entry corresponding to handle from the cache and return.

Parameters:

handle (ResultHandle) – ResultHandle object

Returns:

Cache entry corresponding to handle, if it was present

Return type:

Optional[ResultCache]

process_circuit(circuit: Circuit, n_shots: int | None = None, valid_check: bool = True, **kwargs: int | float | str | None) ResultHandle#

Submit a single circuit to the backend for running. See Backend.process_circuits().

process_circuits(circuits: Sequence[Circuit], n_shots: None | int | Sequence[int | None] = None, valid_check: bool = True, **kwargs: int | float | str | None) List[ResultHandle]#

See pytket.backends.Backend.process_circuits(). Supported kwargs: none.

rebase_pass() BasePass#

A single compilation pass that when run converts all gates in a Circuit to an OpType supported by the Backend (ignoring architecture constraints).

Returns:

Compilation pass that converts gates to primitives supported by Backend.

Return type:

BasePass

run_circuit(circuit: Circuit, n_shots: int | None = None, valid_check: bool = True, **kwargs: int | float | str | None) BackendResult#

Submits a circuit to the backend and returns results

Parameters:
  • circuit – Circuit to be executed

  • n_shots – Passed on to Backend.process_circuit()

  • valid_check – Passed on to Backend.process_circuit()

Returns:

Result

This is a convenience method equivalent to calling Backend.process_circuit() followed by Backend.get_result(). Any additional keyword arguments are passed on to Backend.process_circuit() and Backend.get_result().

run_circuits(circuits: Sequence[Circuit], n_shots: int | Sequence[int] | None = None, valid_check: bool = True, **kwargs: int | float | str | None) List[BackendResult]#

Submits circuits to the backend and returns results

Parameters:
  • circuits – Sequence of Circuits to be executed

  • n_shots – Passed on to Backend.process_circuits()

  • valid_check – Passed on to Backend.process_circuits()

Returns:

List of results

This is a convenience method equivalent to calling Backend.process_circuits() followed by Backend.get_results(). Any additional keyword arguments are passed on to Backend.process_circuits() and Backend.get_results().

valid_circuit(circuit: Circuit) bool#

Checks that the circuit satisfies all of required_predicates.

Parameters:

circuit (Circuit) – The circuit to check.

Returns:

Whether or not all of required_predicates are satisfied.

Return type:

bool

property backend_info: BackendInfo#

Retrieve all Backend properties in a BackendInfo object, including device architecture, supported gate set, gate errors and other hardware-specific information.

Returns:

The BackendInfo describing this backend if it exists.

Return type:

Optional[BackendInfo]

property expectation_allows_nonhermitian: bool#

If expectations are supported, is the operator allowed to be non-Hermitan?

property persistent_handles: bool#

Whether the backend produces ResultHandle objects that can be reused with other instances of the backend class.

property required_predicates: List[Predicate]#

The minimum set of predicates that a circuit must satisfy before it can be successfully run on this backend.

Returns:

Required predicates.

Return type:

List[Predicate]

property supports_contextual_optimisation: bool#

Does this backend support contextual optimisation?

See process_circuits().

property supports_counts: bool#

Does this backend support counts result retrieval via backendresult.BackendResult.get_counts().

property supports_density_matrix: bool#

Does this backend support density matrix retrieval via get_density_matrix.

property supports_expectation: bool#

Does this backend support expectation value calculation for operators.

property supports_shots: bool#

Does this backend support shot result retrieval via backendresult.BackendResult.get_shots().

property supports_state: bool#

Does this backend support statevector retrieval via backendresult.BackendResult.get_state().

property supports_unitary: bool#

Does this backend support unitary retrieval via backendresult.BackendResult.get_unitary().

class pytket.extensions.qsharp.QsharpToffoliSimulatorBackend(backend_name: str = 'Qsharp Backend')#

Backend for simulating a Toffoli circuit using the QDK.

__init__(backend_name: str = 'Qsharp Backend')#
classmethod available_devices(**kwargs: Any) List[BackendInfo]#

Retrieve all available devices as a list of BackendInfo objects, including device name, architecture, supported gate set, gate errors, and other hardware-specific information.

Returns:

A list of BackendInfo objects describing available devices.

Return type:

List[BackendInfo]

cancel(handle: ResultHandle) None#

Cancel a job.

Parameters:

handle (ResultHandle) – handle to job

Raises:

NotImplementedError – If backend does not support job cancellation

circuit_status(handle: ResultHandle) CircuitStatus#

Return a CircuitStatus reporting the status of the circuit execution corresponding to the ResultHandle

default_compilation_pass(optimisation_level: int = 2) BasePass#

A suggested compilation pass that will will, if possible, produce an equivalent circuit suitable for running on this backend.

At a minimum it will ensure that compatible gates are used and that all two- qubit interactions are compatible with the backend’s qubit architecture. At higher optimisation levels, further optimisations may be applied.

This is a an abstract method which is implemented in the backend itself, and so is tailored to the backend’s requirements.

Parameters:

optimisation_level (int, optional) –

The level of optimisation to perform during compilation.

  • Level 0 does the minimum required to solves the device constraints, without any optimisation.

  • Level 1 additionally performs some light optimisations.

  • Level 2 (the default) adds more computationally intensive optimisations that should give the best results from execution.

Returns:

Compilation pass guaranteeing required predicates.

Return type:

BasePass

empty_cache() None#

Manually empty the result cache on the backend.

get_compiled_circuit(circuit: Circuit, optimisation_level: int = 2) Circuit#

Return a single circuit compiled with default_compilation_pass(). See Backend.get_compiled_circuits().

get_compiled_circuits(circuits: Sequence[Circuit], optimisation_level: int = 2) List[Circuit]#

Compile a sequence of circuits with default_compilation_pass() and return the list of compiled circuits (does not act in place).

As well as applying a degree of optimisation (controlled by the optimisation_level parameter), this method tries to ensure that the circuits can be run on the backend (i.e. successfully passed to process_circuits()), for example by rebasing to the supported gate set, or routing to match the connectivity of the device. However, this is not always possible, for example if the circuit contains classical operations that are not supported by the backend. You may use valid_circuit() to check whether the circuit meets the backend’s requirements after compilation. This validity check is included in process_circuits() by default, before any circuits are submitted to the backend.

If the validity check fails, you can obtain more information about the failure by iterating through the predicates in the required_predicates property of the backend, and running the verify() method on each in turn with your circuit.

Parameters:
  • circuits – The circuits to compile.

  • optimisation_level (int, optional) – The level of optimisation to perform during compilation. See default_compilation_pass() for a description of the different levels (0, 1 or 2). Defaults to 2.

Returns:

Compiled circuits.

Return type:

List[Circuit]

get_result(handle: ResultHandle, **kwargs: int | float | str | None) BackendResult#

Return a BackendResult corresponding to the handle.

Use keyword arguments to specify parameters to be used in retrieving results. See specific Backend derived class for available parameters, from the following list:

  • timeout: maximum time to wait for remote job to finish

  • wait: polling interval between remote calls to check job status

Parameters:

handle (ResultHandle) – handle to results

Returns:

Results corresponding to handle.

Return type:

BackendResult

get_results(handles: Iterable[ResultHandle], **kwargs: int | float | str | None) List[BackendResult]#

Return results corresponding to handles.

Parameters:

handles – Iterable of handles

Returns:

List of results

Keyword arguments are as for get_result, and apply to all jobs.

pop_result(handle: ResultHandle) Dict[str, Any] | None#

Remove cache entry corresponding to handle from the cache and return.

Parameters:

handle (ResultHandle) – ResultHandle object

Returns:

Cache entry corresponding to handle, if it was present

Return type:

Optional[ResultCache]

process_circuit(circuit: Circuit, n_shots: int | None = None, valid_check: bool = True, **kwargs: int | float | str | None) ResultHandle#

Submit a single circuit to the backend for running. See Backend.process_circuits().

process_circuits(circuits: Sequence[Circuit], n_shots: None | int | Sequence[int | None] = None, valid_check: bool = True, **kwargs: int | float | str | None) List[ResultHandle]#

See pytket.backends.Backend.process_circuits(). Supported kwargs: none.

rebase_pass() BasePass#

A single compilation pass that when run converts all gates in a Circuit to an OpType supported by the Backend (ignoring architecture constraints).

Returns:

Compilation pass that converts gates to primitives supported by Backend.

Return type:

BasePass

run_circuit(circuit: Circuit, n_shots: int | None = None, valid_check: bool = True, **kwargs: int | float | str | None) BackendResult#

Submits a circuit to the backend and returns results

Parameters:
  • circuit – Circuit to be executed

  • n_shots – Passed on to Backend.process_circuit()

  • valid_check – Passed on to Backend.process_circuit()

Returns:

Result

This is a convenience method equivalent to calling Backend.process_circuit() followed by Backend.get_result(). Any additional keyword arguments are passed on to Backend.process_circuit() and Backend.get_result().

run_circuits(circuits: Sequence[Circuit], n_shots: int | Sequence[int] | None = None, valid_check: bool = True, **kwargs: int | float | str | None) List[BackendResult]#

Submits circuits to the backend and returns results

Parameters:
  • circuits – Sequence of Circuits to be executed

  • n_shots – Passed on to Backend.process_circuits()

  • valid_check – Passed on to Backend.process_circuits()

Returns:

List of results

This is a convenience method equivalent to calling Backend.process_circuits() followed by Backend.get_results(). Any additional keyword arguments are passed on to Backend.process_circuits() and Backend.get_results().

valid_circuit(circuit: Circuit) bool#

Checks that the circuit satisfies all of required_predicates.

Parameters:

circuit (Circuit) – The circuit to check.

Returns:

Whether or not all of required_predicates are satisfied.

Return type:

bool

property backend_info: BackendInfo#

Retrieve all Backend properties in a BackendInfo object, including device architecture, supported gate set, gate errors and other hardware-specific information.

Returns:

The BackendInfo describing this backend if it exists.

Return type:

Optional[BackendInfo]

property expectation_allows_nonhermitian: bool#

If expectations are supported, is the operator allowed to be non-Hermitan?

property persistent_handles: bool#

Whether the backend produces ResultHandle objects that can be reused with other instances of the backend class.

property required_predicates: List[Predicate]#

The minimum set of predicates that a circuit must satisfy before it can be successfully run on this backend.

Returns:

Required predicates.

Return type:

List[Predicate]

property supports_contextual_optimisation: bool#

Does this backend support contextual optimisation?

See process_circuits().

property supports_counts: bool#

Does this backend support counts result retrieval via backendresult.BackendResult.get_counts().

property supports_density_matrix: bool#

Does this backend support density matrix retrieval via get_density_matrix.

property supports_expectation: bool#

Does this backend support expectation value calculation for operators.

property supports_shots: bool#

Does this backend support shot result retrieval via backendresult.BackendResult.get_shots().

property supports_state: bool#

Does this backend support statevector retrieval via backendresult.BackendResult.get_state().

property supports_unitary: bool#

Does this backend support unitary retrieval via backendresult.BackendResult.get_unitary().

class pytket.extensions.qsharp.AzureBackend(target_name: str, resourceId: str | None = None, location: str | None = None, storage: str | None = None, machine_debug: bool = False)#

Backend for running circuits remotely using Azure Quantum devices and simulators. Requires an Azure Quantum workspace set up, as well as the Azure CLI and quantum extension. Requires resourceId and location for Quantum Resource (found when you click on the resource in Azure). This can be provided as a parameter at initialisation or stored in config using pytket.extensions.qsharp.set_azure_config()

Optional parameters can be provided in the same way: storage: The connection string to the Azure storage account. Required if the specified Azure Quantum workspace was not linked to a storage account at workspace creation time.

__init__(target_name: str, resourceId: str | None = None, location: str | None = None, storage: str | None = None, machine_debug: bool = False)#

Intialise a new AzureBackend

Parameters:
  • target_name (str) – name of azure target, e.g. “ionq.simulator”

  • resourceId (Optional[str], optional) – ID for Quantum Resource, can be used to override ID in config file, defaults to None

  • location (Optional[str], optional) – The Azure region where the Azure Quantum workspace is provisioned. This may be specified as a region name such as “East US” or a location name such as “eastus”. If no valid value is specified, defaults to “westus”. Can be used to override ID in config file, defaults to None

  • storage (Optional[str], optional) – The connection string to the Azure storage account, can be used to override ID in config file, defaults to None

Raises:
classmethod available_devices(**kwargs: Any) List[BackendInfo]#

Retrieve all available devices as a list of BackendInfo objects, including device name, architecture, supported gate set, gate errors, and other hardware-specific information.

Returns:

A list of BackendInfo objects describing available devices.

Return type:

List[BackendInfo]

cancel(handle: ResultHandle) None#

Cancel a job.

Parameters:

handle (ResultHandle) – handle to job

Raises:

NotImplementedError – If backend does not support job cancellation

circuit_status(handle: ResultHandle) CircuitStatus#

Return a CircuitStatus reporting the status of the circuit execution corresponding to the ResultHandle

default_compilation_pass(optimisation_level: int = 2) BasePass#

A suggested compilation pass that will will, if possible, produce an equivalent circuit suitable for running on this backend.

At a minimum it will ensure that compatible gates are used and that all two- qubit interactions are compatible with the backend’s qubit architecture. At higher optimisation levels, further optimisations may be applied.

This is a an abstract method which is implemented in the backend itself, and so is tailored to the backend’s requirements.

Parameters:

optimisation_level (int, optional) –

The level of optimisation to perform during compilation.

  • Level 0 does the minimum required to solves the device constraints, without any optimisation.

  • Level 1 additionally performs some light optimisations.

  • Level 2 (the default) adds more computationally intensive optimisations that should give the best results from execution.

Returns:

Compilation pass guaranteeing required predicates.

Return type:

BasePass

empty_cache() None#

Manually empty the result cache on the backend.

get_compiled_circuit(circuit: Circuit, optimisation_level: int = 2) Circuit#

Return a single circuit compiled with default_compilation_pass(). See Backend.get_compiled_circuits().

get_compiled_circuits(circuits: Sequence[Circuit], optimisation_level: int = 2) List[Circuit]#

Compile a sequence of circuits with default_compilation_pass() and return the list of compiled circuits (does not act in place).

As well as applying a degree of optimisation (controlled by the optimisation_level parameter), this method tries to ensure that the circuits can be run on the backend (i.e. successfully passed to process_circuits()), for example by rebasing to the supported gate set, or routing to match the connectivity of the device. However, this is not always possible, for example if the circuit contains classical operations that are not supported by the backend. You may use valid_circuit() to check whether the circuit meets the backend’s requirements after compilation. This validity check is included in process_circuits() by default, before any circuits are submitted to the backend.

If the validity check fails, you can obtain more information about the failure by iterating through the predicates in the required_predicates property of the backend, and running the verify() method on each in turn with your circuit.

Parameters:
  • circuits – The circuits to compile.

  • optimisation_level (int, optional) – The level of optimisation to perform during compilation. See default_compilation_pass() for a description of the different levels (0, 1 or 2). Defaults to 2.

Returns:

Compiled circuits.

Return type:

List[Circuit]

get_result(handle: ResultHandle, **kwargs: int | float | str | None) BackendResult#

See pytket.backends.Backend.get_result(). Supported kwargs: timeout, wait.

get_results(handles: Iterable[ResultHandle], **kwargs: int | float | str | None) List[BackendResult]#

Return results corresponding to handles.

Parameters:

handles – Iterable of handles

Returns:

List of results

Keyword arguments are as for get_result, and apply to all jobs.

pop_result(handle: ResultHandle) Dict[str, Any] | None#

Remove cache entry corresponding to handle from the cache and return.

Parameters:

handle (ResultHandle) – ResultHandle object

Returns:

Cache entry corresponding to handle, if it was present

Return type:

Optional[ResultCache]

process_circuit(circuit: Circuit, n_shots: int | None = None, valid_check: bool = True, **kwargs: int | float | str | None) ResultHandle#

Submit a single circuit to the backend for running. See Backend.process_circuits().

process_circuits(circuits: Sequence[Circuit], n_shots: None | int | Sequence[int | None] = None, valid_check: bool = True, **kwargs: int | float | str | None) List[ResultHandle]#

See pytket.backends.Backend.process_circuits(). Supported kwargs: postprocess.

rebase_pass() BasePass#

A single compilation pass that when run converts all gates in a Circuit to an OpType supported by the Backend (ignoring architecture constraints).

Returns:

Compilation pass that converts gates to primitives supported by Backend.

Return type:

BasePass

run_circuit(circuit: Circuit, n_shots: int | None = None, valid_check: bool = True, **kwargs: int | float | str | None) BackendResult#

Submits a circuit to the backend and returns results

Parameters:
  • circuit – Circuit to be executed

  • n_shots – Passed on to Backend.process_circuit()

  • valid_check – Passed on to Backend.process_circuit()

Returns:

Result

This is a convenience method equivalent to calling Backend.process_circuit() followed by Backend.get_result(). Any additional keyword arguments are passed on to Backend.process_circuit() and Backend.get_result().

run_circuits(circuits: Sequence[Circuit], n_shots: int | Sequence[int] | None = None, valid_check: bool = True, **kwargs: int | float | str | None) List[BackendResult]#

Submits circuits to the backend and returns results

Parameters:
  • circuits – Sequence of Circuits to be executed

  • n_shots – Passed on to Backend.process_circuits()

  • valid_check – Passed on to Backend.process_circuits()

Returns:

List of results

This is a convenience method equivalent to calling Backend.process_circuits() followed by Backend.get_results(). Any additional keyword arguments are passed on to Backend.process_circuits() and Backend.get_results().

valid_circuit(circuit: Circuit) bool#

Checks that the circuit satisfies all of required_predicates.

Parameters:

circuit (Circuit) – The circuit to check.

Returns:

Whether or not all of required_predicates are satisfied.

Return type:

bool

property backend_info: BackendInfo#

Retrieve all Backend properties in a BackendInfo object, including device architecture, supported gate set, gate errors and other hardware-specific information.

Returns:

The BackendInfo describing this backend if it exists.

Return type:

Optional[BackendInfo]

property expectation_allows_nonhermitian: bool#

If expectations are supported, is the operator allowed to be non-Hermitan?

property persistent_handles: bool#

Whether the backend produces ResultHandle objects that can be reused with other instances of the backend class.

property required_predicates: List[Predicate]#

The minimum set of predicates that a circuit must satisfy before it can be successfully run on this backend.

Returns:

Required predicates.

Return type:

List[Predicate]

property supports_contextual_optimisation: bool#

Does this backend support contextual optimisation?

See process_circuits().

property supports_counts: bool#

Does this backend support counts result retrieval via backendresult.BackendResult.get_counts().

property supports_density_matrix: bool#

Does this backend support density matrix retrieval via get_density_matrix.

property supports_expectation: bool#

Does this backend support expectation value calculation for operators.

property supports_shots: bool#

Does this backend support shot result retrieval via backendresult.BackendResult.get_shots().

property supports_state: bool#

Does this backend support statevector retrieval via backendresult.BackendResult.get_state().

property supports_unitary: bool#

Does this backend support unitary retrieval via backendresult.BackendResult.get_unitary().

Module for conversion from tket to Q#

pytket.extensions.qsharp.tk_to_qsharp(tkcirc: Circuit, sim: bool = True) str#

Convert a tket Circuit to a Q# program.

Parameters:

tkcirc – Circuit to be converted

Returns:

Converted circuit

class pytket.extensions.qsharp.backends.config.QSharpConfig(resourceId: str | None, location: str | None, storage: str | None)#

Holds config parameters for pytket-qsharp.

classmethod from_extension_dict(ext_dict: Dict[str, Any]) QSharpConfig#

Abstract method to build PytketExtConfig from dictionary serialized form.

pytket.extensions.qsharp.backends.config.set_azure_config(resourceId: str | None = None, location: str | None = None, storage: str | None = None) None#

Set default values for any of resourceId, location or storage for your Azure Quantum Workspace. Can be overridden in backend construction.