pytket-qiskit#
IBM’s Qiskit is an open-source framework for quantum computation, ranging from high-level algorithms to low-level circuit representations, simulation and access to the IBMQ Experience devices.
pytket-qiskit
is an extension to pytket
that allows pytket
circuits to be
run on IBM backends and simulators, as well as conversion to and from Qiskit
representations.
pytket-qiskit
is available for Python 3.9, 3.10 and 3.11, on Linux, MacOS and
Windows. To install, run:
pip install pytket-qiskit
This will install pytket
if it isn’t already installed, and add new classes
and methods into the pytket.extensions
namespace.
An example using the shots-based AerBackend
simulator is shown below.
from pytket.extensions.qiskit import AerBackend
from pytket import Circuit
backend = AerBackend()
circ = Circuit(2).H(0).CX(0, 1).measure_all()
# Compilation not needed here as both H and CX are supported gates
result = backend.run_circuit(circ, n_shots=1000)
This simulator supports a large set of gates and by default has no architectural constraints or quantum noise. However the user can pass in a noise model or custom architecture to more closely model a real quantum device.
The AerBackend
also supports GPU simulation which can be configured as follows.
from pytket.extensions.qiskit import AerBackend
backend = AerBackend()
backend._qiskit_backend.set_option("device", "GPU")
Note
Making use of GPU simulation requires the qiskit-aer-gpu package. This can be installed with the command
pip install qiskit-aer-gpu
Access and Credentials#
With the exception of the Aer simulators, accessing devices and simulators through the pytket-qiskit
extension requires an IBM account. An account can be set up here: https://quantum-computing.ibm.com/login.
Once you have created an account you can obtain an API token which you can use to configure your credentials locally.
In this section we are assuming that you have set the following variables with the corresponding values:
# Replace the placeholders with your actual values
ibm_token = '<your_ibm_token_here>'
hub = '<your_hub_here>'
group = '<your_group_here>'
project = '<your_project_here>'
Note
The documentation below is correct as of pytket-qiskit version 0.40.0 and newer. In the 0.40.0 release pytket-qiskit moved to using the qiskit-ibm-provider. In pytket-qiskit versions 0.39.0 and older the parameters hub
, group
and project
were handled separately instead of a single instance
string as in 0.40.0 and newer.
from pytket.extensions.qiskit import set_ibmq_config
set_ibmq_config(ibmq_api_token=ibm_token)
After saving your credentials you can access pytket-qiskit
backend repeatedly without having to re-initialise your credentials.
If you are a member of an IBM hub then you can add this information to set_ibmq_config
as well.
from pytket.extensions.qiskit import set_ibmq_config
set_ibmq_config(ibmq_api_token=ibm_token, instance=f"{hub}/{group}/{project}")
Alternatively you can use the following qiskit commands to save your credentials locally without saving the token in pytket config:
Note
If using pytket-qiskit 0.39.0 or older you will have to use the deprecated IBMQ.save_account()
instead of IBMProvider.save_account()
in the code below.
from qiskit_ibm_provider import IBMProvider
from qiskit_ibm_runtime import QiskitRuntimeService
IBMProvider.save_account(token=ibm_token)
QiskitRuntimeService.save_account(channel="ibm_quantum", token=ibm_token)
To see which devices you can access you can use the available_devices
method on the IBMQBackend
or IBMQEmulatorBackend
. Note that it is possible to pass optional instance
and provider
arguments to this method. This allows you to see which devices are accessible through your IBM hub.
from pytket.extensions.qiskit import IBMQBackend
from qiskit_ibm_provider import IBMProvider
my_instance=f"{hub}/{group}/{project}"
ibm_provider = IBMProvider(instance=my_instance)
backend = IBMQBackend("ibmq_nairobi") # Initialise backend for an IBM device
backendinfo_list = backend.available_devices(instance=my_instance, provider=ibm_provider)
print([backend.device_name for backend in backendinfo_list])
Backends Available Through pytket-qiskit#
The pytket-qiskit
extension has several types of available Backend
. These are the IBMQBackend
and several types of simulator.
Backend |
Type |
---|---|
Interface to an IBM quantum computer. |
|
Emulator for a chosen |
|
A noiseless, shots-based simulator for quantum circuits [1] |
|
Statevector simulator. |
|
Unitary simulator |
[1]
AerBackend
is noiseless by default and has no architecture. However it can accept a user definedNoiseModel
andArchitecture
.In addition to the backends above the
pytket-qiskit
extension also has theTketBackend
. This allows a tketBackend
to be used directly through qiskit. see the notebook example on qiskit integration.
Default Compilation#
Every Backend
in pytket has its own default_compilation_pass
method. This method applies a sequence of optimisations to a circuit depending on the value of an optimisation_level
parameter. This default compilation will ensure that the circuit meets all the constraints required to run on the Backend
. The passes applied by different levels of optimisation are specified in the table below.
optimisation_level = 0 |
optimisation_level = 1 |
optimisation_level = 2 [1] |
---|---|---|
self.rebase_pass [2] |
||
CXMappingPass [3] |
CXMappingPass [3] |
CXMappingPass [3] |
self.rebase_pass [2] |
||
self.rebase_pass [2] |
||
self.rebase_pass [2] |
||
[1] If no value is specified then
optimisation_level
defaults to a value of 2.[2] self.rebase_pass is a rebase to the gateset supported by the backend, For IBM quantum devices that is {X, SX, Rz, CX}.
[3] Here
CXMappingPass
maps program qubits to the architecture using a NoiseAwarePlacement
Note: The default_compilation_pass
for AerBackend
is the same as above.
Backend Predicates#
Circuits must satisfy certain conditions before they can be processed on a device or simulator. In pytket these conditions are called predicates.
All pytket-qiskit
backends have the following two predicates.
GateSetPredicate - The circuit must contain only operations supported by the :py:class`Backend`. To view supported Ops run
BACKENDNAME.backend_info.gate_set
.NoSymbolsPredicate - Parameterised gates must have numerical values when the circuit is executed.
The IBMQBackend
and IBMQEmulatorBackend
may also have the following predicates depending on the capabilities of the specified device.
- API documentation
IBMQBackend
IBMQEmulatorBackend
IBMQEmulatorBackend.__init__()
IBMQEmulatorBackend.cancel()
IBMQEmulatorBackend.circuit_status()
IBMQEmulatorBackend.default_compilation_pass()
IBMQEmulatorBackend.get_result()
IBMQEmulatorBackend.process_circuits()
IBMQEmulatorBackend.rebase_pass()
IBMQEmulatorBackend.backend_info
IBMQEmulatorBackend.required_predicates
AerBackend
AerBackend.__init__()
AerBackend.available_devices()
AerBackend.cancel()
AerBackend.circuit_status()
AerBackend.default_compilation_pass()
AerBackend.empty_cache()
AerBackend.get_compiled_circuit()
AerBackend.get_compiled_circuits()
AerBackend.get_operator_expectation_value()
AerBackend.get_pauli_expectation_value()
AerBackend.get_result()
AerBackend.get_results()
AerBackend.pop_result()
AerBackend.process_circuit()
AerBackend.process_circuits()
AerBackend.rebase_pass()
AerBackend.run_circuit()
AerBackend.run_circuits()
AerBackend.valid_circuit()
AerBackend.backend_info
AerBackend.expectation_allows_nonhermitian
AerBackend.persistent_handles
AerBackend.required_predicates
AerBackend.supports_contextual_optimisation
AerBackend.supports_counts
AerBackend.supports_density_matrix
AerBackend.supports_expectation
AerBackend.supports_shots
AerBackend.supports_state
AerBackend.supports_unitary
AerStateBackend
AerStateBackend.__init__()
AerStateBackend.available_devices()
AerStateBackend.cancel()
AerStateBackend.circuit_status()
AerStateBackend.default_compilation_pass()
AerStateBackend.empty_cache()
AerStateBackend.get_compiled_circuit()
AerStateBackend.get_compiled_circuits()
AerStateBackend.get_operator_expectation_value()
AerStateBackend.get_pauli_expectation_value()
AerStateBackend.get_result()
AerStateBackend.get_results()
AerStateBackend.pop_result()
AerStateBackend.process_circuit()
AerStateBackend.process_circuits()
AerStateBackend.rebase_pass()
AerStateBackend.run_circuit()
AerStateBackend.run_circuits()
AerStateBackend.valid_circuit()
AerStateBackend.backend_info
AerStateBackend.expectation_allows_nonhermitian
AerStateBackend.persistent_handles
AerStateBackend.required_predicates
AerStateBackend.supports_contextual_optimisation
AerStateBackend.supports_counts
AerStateBackend.supports_density_matrix
AerStateBackend.supports_expectation
AerStateBackend.supports_shots
AerStateBackend.supports_state
AerStateBackend.supports_unitary
AerUnitaryBackend
AerUnitaryBackend.__init__()
AerUnitaryBackend.available_devices()
AerUnitaryBackend.cancel()
AerUnitaryBackend.circuit_status()
AerUnitaryBackend.default_compilation_pass()
AerUnitaryBackend.empty_cache()
AerUnitaryBackend.get_compiled_circuit()
AerUnitaryBackend.get_compiled_circuits()
AerUnitaryBackend.get_operator_expectation_value()
AerUnitaryBackend.get_pauli_expectation_value()
AerUnitaryBackend.get_result()
AerUnitaryBackend.get_results()
AerUnitaryBackend.pop_result()
AerUnitaryBackend.process_circuit()
AerUnitaryBackend.process_circuits()
AerUnitaryBackend.rebase_pass()
AerUnitaryBackend.run_circuit()
AerUnitaryBackend.run_circuits()
AerUnitaryBackend.valid_circuit()
AerUnitaryBackend.backend_info
AerUnitaryBackend.expectation_allows_nonhermitian
AerUnitaryBackend.persistent_handles
AerUnitaryBackend.required_predicates
AerUnitaryBackend.supports_contextual_optimisation
AerUnitaryBackend.supports_counts
AerUnitaryBackend.supports_density_matrix
AerUnitaryBackend.supports_expectation
AerUnitaryBackend.supports_shots
AerUnitaryBackend.supports_state
AerUnitaryBackend.supports_unitary
process_characterisation()
qiskit_to_tk()
tk_to_qiskit()
TketBackend
TketAutoPass
TketPass
JobInfo
TketJob
QiskitConfig
set_ibmq_config()
- Changelog
- 0.46.0 (November 2023)
- 0.45.0 (October 2023)
- 0.44.0 (September 2023)
- 0.43.0 (August 2023)
- 0.42.0 (August 2023)
- 0.41.0 (July 2023)
- 0.40.0 (June 2023)
- 0.39.0 (May 2023)
- 0.38.0 (April 2023)
- 0.37.1 (March 2023)
- 0.37.0 (March 2023)
- 0.36.0 (February 2023)
- 0.35.0 (February 2023)
- 0.34.0 (January 2023)
- 0.33.0 (December 2022)
- 0.32.0 (December 2022)
- 0.31.0 (November 2022)
- 0.30.0 (November 2022)
- 0.29.0 (October 2022)
- 0.28.0 (August 2022)
- 0.27.0 (July 2022)
- 0.26.0 (June 2022)
- 0.25.0 (May 2022)
- 0.24.0 (April 2022)
- 0.23.0 (March 2022)
- 0.22.2 (February 2022)
- 0.22.1 (February 2022)
- 0.22.0 (February 2022)
- 0.21.0 (January 2022)
- 0.20.0 (November 2021)
- 0.19.0 (October 2021)
- 0.18.0 (September 2021)
- 0.17.0 (September 2021)
- 0.16.1 (July 2021)
- 0.16.0 (July 2021)
- 0.15.1 (July 2021)
- 0.15.0 (June 2021)
- 0.14.0 (unreleased)
- 0.13.0 (May 2021)
- 0.12.0 (unreleased)
- 0.11.0 (May 2021)
- 0.10.0 (April 2021)
pytket documentation:
Links: