Skip to content

Commit

Permalink
Merge pull request #159 from claretgrace0801/qiskit-pulse-simulators
Browse files Browse the repository at this point in the history
Pulse Simulator backends for qiskit
  • Loading branch information
BoxiLi committed Oct 19, 2022
2 parents d32bd0f + 89e7fb7 commit 5556890
Show file tree
Hide file tree
Showing 13 changed files with 698 additions and 108 deletions.
1 change: 1 addition & 0 deletions doc/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ docutils==0.17.1
sphinxcontrib-bibtex==2.4.2
pyqir-generator==0.6.2
pyqir-parser==0.6.2
qiskit==0.37.2
10 changes: 10 additions & 0 deletions doc/source/apidoc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,13 @@ Simulation based on the master equation.
qutip_qip.compiler
qutip_qip.pulse
qutip_qip.noise

Qiskit Circuit Simulation
--------------------------
Simulation of qiskit circuits based on qutip_qip backends.

.. autosummary::
:toctree: apidoc/
:template: autosummary/module.rst

qutip_qip.qiskit
25 changes: 25 additions & 0 deletions doc/source/apidoc/qutip_qip.qiskit.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
:orphan:

qutip\_qip.qiskit
===========================

.. automodule:: qutip_qip.qiskit
:members:
:show-inheritance:
:imported-members:

.. rubric:: Classes

.. autosummary::

QiskitSimulatorBase
QiskitCircuitSimulator
QiskitPulseSimulator
Provider
Job

.. rubric:: Fucntions

.. autosummary::

convert_qiskit_circuit
1 change: 1 addition & 0 deletions doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ qutip-qip |version|: QuTiP quantum information processing
qip-simulator.rst
qip-processor.rst
qip-vqa.rst
qip-qiskit.rst

.. toctree::
:maxdepth: 2
Expand Down
206 changes: 206 additions & 0 deletions doc/source/qip-qiskit.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
.. _qip_qiskit:

**********************************
`qutip-qip` as a Qiskit backend
**********************************

This submodule was implemented by `Shreyas Pradhan <shpradhan12@gmail.com>`_ as part of Google Summer of Code 2022.

Overview
===============

This submodule provides an interface to simulate circuits made in qiskit.

Gate-level simulation on qiskit circuits is possible with :class:`.QiskitCircuitSimulator`. Pulse-level simulation is possible with :class:`.QiskitPulseSimulator` which supports simulation using the :class:`.LinearSpinChain`, :class:`.CircularSpinChain` and :class:`.DispersiveCavityQED` pulse processors.

Running a qiskit circuit with `qutip_qip`
==========================================

After constructing a circuit in qiskit, either of the qutip_qip based backends (:class:`.QiskitCircuitSimulator` and :class:`.QiskitPulseSimulator`) can be used to run that circuit.

Example
--------

Let's try constructing and simulating a qiskit circuit.

We define a simple circuit as follows:


.. plot::
:context: close-figs

.. doctest::
:hide:

>>> import random
>>> random.seed(1)

.. doctest::
:options: +SKIP

>>> from qiskit import QuantumCircuit
>>> circ = QuantumCircuit(2,2)

>>> circ.h(0)
>>> circ.h(1)
>>> circ.measure(0,0)
>>> circ.measure(1,1)

Let's run this on the :class:`.QiskitCircuitSimulator` backend:

.. doctest::
:options: +SKIP

>>> from qutip_qip.qiskit import QiskitCircuitSimulator
>>> backend = QiskitCircuitSimulator()
>>> job = backend.run(circ)
>>> result = job.result()

The result object inherits from the :class:`qiskit.result.Result` class. Hence, we can use it's functions like ``result.get_counts()`` as required. We can also access the final state with ``result.data()['statevector']``.

.. code-block::
>>> result.data()['statevector']
Statevector([0.+0.j, 1.+0.j, 0.+0.j, 0.+0.j], dims=(2, 2))
.. doctest::
:options: +SKIP

>>> from qiskit.visualization import plot_histogram
>>> plot_histogram(result.get_counts())

.. plot::
:context: close-figs

Now, let's run the same circuit on :class:`.QiskitPulseSimulator`.

While using a pulse processor, we define the circuit without measurements.

.. note::

The pulse-level simulator does not support measurement. Please use :obj:`qutip.measure` to process the result manually. By default, all the qubits will be measured at the end of the circuit.

.. _pulse circ:

.. doctest::
:options: +SKIP

>>> pulse_circ = QuantumCircuit(2,2)
>>> pulse_circ.h(0)
>>> pulse_circ.h(1)

To use the :class:`.QiskitPulseSimulator` backend, we need to define the processor on which we want to run the circuit. This includes defining the pulse processor model with all the required parameters including noise.

Different hardware parameters can be supplied here for :obj:`.LinearSpinChain`. Please refer to the documentation for details.

.. doctest::
:options: +SKIP

>>> from qutip_qip.device import LinearSpinChain
>>> processor = LinearSpinChain(num_qubits=2)

Now that we defined our processor (:class:`.LinearSpinChain` in this case), we can use it to perform the simulation:

.. doctest::
:options: +SKIP

>>> from qutip_qip.qiskit import QiskitPulseSimulator

>>> pulse_backend = QiskitPulseSimulator(processor)
>>> pulse_job = pulse_backend.run(pulse_circ)
>>> pulse_result = pulse_job.result()

.. _pulse plot:

.. doctest::
:options: +SKIP

>>> plot_histogram(pulse_result.get_counts())


Configurable Options
========================

Qiskit's interface allows us to provide some options like ``shots`` while running a circuit on a backend. We also have provided some options for the qutip_qip backends.

``shots``
-------------
``shots`` is the number of times measurements are sampled from the simulation result. By default it is set to ``1024``.

``allow_custom_gate``
-----------------------
``allow_custom_gate``, when set to ``False``, does not allowing simulating circuits that have user-defined gates; it will throw an error in that case. By default, it is set to ``True``, in which case, the backend will simulate a user-defined gate by computing its unitary matrix.

.. note::

Although you can pass this option while running a circuit on pulse backends, you need to make sure that the gate is supported by the backend simulator :obj:`.Processor` in ``qutip-qip``.

An example demonstrating configuring options:

.. doctest::

backend = QiskitCircuitSimulator()
job = backend.run(circ, shots=3000)
result = job.result()

We provided the value of shots explicitly, hence our options for the simulation are set as: ``shots=3000`` and ``allow_custom_gate=True``.

Another example:

.. doctest::

backend = QiskitCircuitSimulator()
job = backend.run(circ, shots=3000, allow_custom_gate=False)
result = job.result()


Noise
=======

Real quantum devices are not ideal and are bound to have some amount of noise in them. One of the uses of having the pulse backends is the ability to add noise to our device.

Let's look at an example where we add some noise to our circuit and see what kind of bias it has on the results. We'll use the same circuit we used :ref:`above<pulse circ>`.

Let's use the :class:`.CircularSpinChain` processor this time with some noise.

.. plot::
:context: close-figs

.. doctest::
:options: +SKIP

>>> from qutip_qip.device import CircularSpinChain
>>> processor = CircularSpinChain(num_qubits=2, t1=0.3)

If we ran this on a processor without noise we would expect all states to be approximately equiprobable, like we saw :ref:`above<pulse plot>`.

.. doctest::
:options: +SKIP

>>> noisy_backend = QiskitPulseSimulator(processor)
>>> noisy_job = noisy_backend.run(pulse_circ)
>>> noisy_result = noisy_job.result()

``t1=0.3`` will cause amplitude damping on all qubits, and hence, ``0`` is more probable than ``1`` in the final output for all qubits.

We can see what the result looks like in the density matrix format:

.. code-block::
>>> noisy_result.data()['statevector']
DensityMatrix([[ 0.4484772 +0.00000000e+00j, 0.04130281+2.46325222e-01j,
0.04130281+2.46325222e-01j, -0.13148987+4.53709696e-02j],
[ 0.04130281-2.46325222e-01j, 0.22120721+0.00000000e+00j,
0.13909747-1.10349672e-17j, 0.02037223+1.21497634e-01j],
[ 0.04130281-2.46325222e-01j, 0.13909747+1.10349672e-17j,
0.22120721+0.00000000e+00j, 0.02037223+1.21497634e-01j],
[-0.13148987-4.53709696e-02j, 0.02037223-1.21497634e-01j,
0.02037223-1.21497634e-01j, 0.10910838+0.00000000e+00j]],
dims=(2, 2))
.. doctest::
:options: +SKIP

>>> plot_histogram(noisy_result.get_counts())


46 changes: 43 additions & 3 deletions src/qutip_qip/device/cavityqed.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ def __init__(
self, num_qubits, num_levels=10, correct_global_phase=True, **params
):
model = CavityQEDModel(
num_qubits=num_qubits,
num_levels=num_levels,
**params,
num_qubits=num_qubits, num_levels=num_levels, **params
)
super(DispersiveCavityQED, self).__init__(
model=model, correct_global_phase=correct_global_phase
Expand Down Expand Up @@ -167,6 +165,48 @@ def load_circuit(self, qc, schedule_mode="ASAP", compiler=None):
self.global_phase = compiler.global_phase
return tlist, coeff

def generate_init_processor_state(
self, init_circuit_state: Qobj = None
) -> Qobj:
"""
Generate the initial state with the dimensions of the :class:`.DispersiveCavityQED` processor.
Parameters
----------
init_circuit_state : :class:`qutip.Qobj`
Initial state provided with the dimensions of the circuit.
Returns
-------
:class:`qutip.Qobj`
Return the initial state with the dimensions of the :class:`.DispersiveCavityQED` processor model.
If initial_circuit_state was not provided, return the zero state.
"""
if init_circuit_state is None:
return basis(
[self.num_levels] + [2] * self.num_qubits,
[0] + [0] * self.num_qubits,
)
return tensor(basis(self.num_levels, 0), init_circuit_state)

def get_final_circuit_state(self, final_processor_state: Qobj) -> Qobj:
"""
Truncate the final processor state to get rid of the cavity subsystem.
Parameters
----------
final_processor_state : :class:`qutip.Qobj`
State provided with the dimensions of the DispersiveCavityQED processor model.
Returns
-------
:class:`qutip.Qobj`
Return the truncated final state with the dimensions of the circuit.
"""
return final_processor_state.ptrace(
range(1, len(final_processor_state.dims[0]))
)


class CavityQEDModel(Model):
"""
Expand Down
41 changes: 40 additions & 1 deletion src/qutip_qip/device/modelprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import numpy as np

from qutip import Qobj, QobjEvo, tensor, mesolve
from qutip import Qobj, QobjEvo, tensor, mesolve, basis
from ..operations import globalphase
from ..circuit import QubitCircuit
from .processor import Processor
Expand Down Expand Up @@ -243,6 +243,45 @@ def load_circuit(self, qc, schedule_mode="ASAP", compiler=None):
self.set_tlist(tlist)
return tlist, coeffs

def generate_init_processor_state(
self, init_circuit_state: Qobj = None
) -> Qobj:
"""
Generate the initial state with the dimensions of the processor.
Parameters
----------
init_circuit_state : :class:`qutip.Qobj`
Initial state provided with the dimensions of the circuit.
Returns
-------
:class:`qutip.Qobj`
Return the initial state with the dimensions
of the processor model. If initial_circuit_state
was not provided, return the zero state.
"""
if init_circuit_state is None:
return basis([2] * self.num_qubits, [0] * self.num_qubits)
return init_circuit_state

def get_final_circuit_state(self, final_processor_state: Qobj) -> Qobj:
"""
Convert the state with the dimensions of the processor model to
a state with the dimensions of the circuit.
Parameters
----------
final_processor_state : :class:`qutip.Qobj`
State provided with the dimensions of the processor model.
Returns
-------
:class:`qutip.Qobj`
Return the final state with the dimensions of the circuit.
"""
return final_processor_state


def _to_array(params, num_qubits):
"""
Expand Down
9 changes: 9 additions & 0 deletions src/qutip_qip/qiskit/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
"""Simulation of qiskit circuits in ``qutip_qip``."""

from .provider import Provider
from .backend import (
QiskitSimulatorBase,
QiskitCircuitSimulator,
QiskitPulseSimulator,
)
from .converter import convert_qiskit_circuit
from .job import Job

0 comments on commit 5556890

Please sign in to comment.