Getting started¶
Once the PennyLane-Qiskit plugin is installed, the three provided Qiskit devices can be accessed straightaway in PennyLane.
You can instantiate a 'qiskit.aer'
device for PennyLane with:
import pennylane as qml
dev = qml.device('qiskit.aer', wires=2)
This device can then be used just like other devices for the definition and evaluation of QNodes within PennyLane. A simple quantum function that returns the expectation value of a measurement and depends on three classical input parameters would look like:
@qml.qnode(dev)
def circuit(x, y, z):
qml.RZ(z, wires=[0])
qml.RY(y, wires=[0])
qml.RX(x, wires=[0])
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliZ(wires=1))
You can then execute the circuit like any other function to get the quantum mechanical expectation value.
circuit(0.2, 0.1, 0.3)
You can also change the default device’s backend with
dev = qml.device('qiskit.aer', wires=2, backend='unitary_simulator')
To get a current overview what backends are available you can query this by
dev.capabilities()['backend']
While the device 'qiskit.aer'
is the standard go-to simulator that is provided along
the Qiskit main package installation, there exists a natively included python simulator
that is slower but will work usually without the need to check out other dependencies
(gcc, blas and so on) which can be used by 'qiskit.basicaer'
.
Another important difference between the two is that while 'qiskit.aer'
supports a simulation with noise, 'qiskit.basicaer'
does not.
Noise models¶
You can instantiate a noise model and apply it to the device by calling
import pennylane as qml
import qiskit
from qiskit.providers.aer.noise.device import basic_device_noise_model
qiskit.IBMQ.load_account()
provider = qiskit.IBMQ.get_provider(group='open')
ibmq_16_melbourne = provider.get_backend('ibmq_16_melbourne')
device_properties = ibmq_16_melbourne.properties()
noise_model = basic_device_noise_model(device_properties)
dev = qml.device('qiskit.aer', wires=2, noise_model=noise_model)
Please refer to the Qiskit documentation for more information on noise models.
IBM Q Experience¶
PennyLane-Qiskit supports running PennyLane on IBM Q hardware via the qistkit.ibmq
device.
You can choose between different backends - either simulators or real hardware.
import pennylane as qml
dev = qml.device('qiskit.ibmq', wires=2, backend='ibmq_16_melbourne')
By default, the qiskit.ibmq
device will attempt to use an already active or stored
IBM Q account. If none are available, you may also directly pass your IBM Q API token,
as well as an optional URL:
import pennylane as qml
dev = qml.device('qiskit.ibmq', wires=2, backend='ibmq_qasm_simulator', ibmqx_token="XXX")
In order to avoid accidentally publishing your token, it is best to store it using the
qiskit.IBMQ.save_account()
function. Alternatively, you can specify the token or URL via the
PennyLane configuration file by
adding a section such as
[qiskit.global]
[qiskit.ibmq]
ibmqx_token = "XXX"
ibmqx_url = "XXX"
Note that, by default, the qiskit.ibmq
device uses the simulator backend
ibmq_qasm_simulator
, but this may be changed to any of the real backends as given by
dev.capabilities()['backend']