Demo for user defined gates

User defined gate
In [1]:
from qutip import *
import numpy as np

User defined gates

This demo is based on the branch https://github.com/BoxiLi/qutip/tree/user_gate

A user defined gate can be specified by a python function that takes at most one parameter and return a Qobj, the dimension of the Qobj has to match the qubit system.

In [2]:
def user_gate1(arg_values):
    # controlled rotation X
    mat = np.zeros((4, 4), dtype=np.complex)
    mat[0, 0] = mat[1, 1] = 1.
    mat[2:4, 2:4] = rx(arg_values)
    return Qobj(mat, dims=[[2, 2], [2, 2]])

def user_gate2():
    # T gate
    mat = np.array([[1.,   0],
                    [0., 1.j]])
    return Qobj(mat, dims=[[2], [2]])

To let the QubitCircuit process those gates, one can modify its the attributes QubitCircuit.user_gates, which is a python dictionary in the form {name: gate_function}.

In [3]:
qc = QubitCircuit(2)
qc.user_gates = {"CTRLRX": user_gate1, 
                 "T"     : user_gate2}

When calling the add_gate method, the targets qubits and the argument need to be given.

In [4]:
# qubit 0 controlls qubit 1
qc.add_gate("CTRLRX", targets=[0,1], arg_value=np.pi/2)
# qubit 1 controlls qutbi 0
qc.add_gate("CTRLRX", targets=[1,0], arg_value=np.pi/2)
qc.add_gate("T", targets=[1])
props = qc.propagators()
In [5]:
props[0] # qubit 0 controlls qubit 1
Out[5]:
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = False\begin{equation*}\left(\begin{array}{*{11}c}1.0 & 0.0 & 0.0 & 0.0\\0.0 & 1.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.707 & -0.707j\\0.0 & 0.0 & -0.707j & 0.707\\\end{array}\right)\end{equation*}
In [6]:
props[1] # qubit 1 controlls qutbi 0
Out[6]:
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = False\begin{equation*}\left(\begin{array}{*{11}c}1.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.707 & 0.0 & -0.707j\\0.0 & 0.0 & 1.0 & 0.0\\0.0 & -0.707j & 0.0 & 0.707\\\end{array}\right)\end{equation*}
In [7]:
props[2] # T gate acts on qubit 1
Out[7]:
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = False\begin{equation*}\left(\begin{array}{*{11}c}1.0 & 0.0 & 0.0 & 0.0\\0.0 & 1.0j & 0.0 & 0.0\\0.0 & 0.0 & 1.0 & 0.0\\0.0 & 0.0 & 0.0 & 1.0j\\\end{array}\right)\end{equation*}

molmer sorensen gate and qubit rotation gate

In [8]:
molmer_sorensen(np.pi/2, targets=[0, 1])
Out[8]:
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = False\begin{equation*}\left(\begin{array}{*{11}c}0.707 & 0.0 & 0.0 & -0.707j\\0.0 & 0.707 & -0.707j & 0.0\\0.0 & -0.707j & 0.707 & 0.0\\-0.707j & 0.0 & 0.0 & 0.707\\\end{array}\right)\end{equation*}
In [9]:
molmer_sorensen(np.pi/2, N=3, targets=[1,2])
Out[9]:
Quantum object: dims = [[2, 2, 2], [2, 2, 2]], shape = (8, 8), type = oper, isherm = False\begin{equation*}\left(\begin{array}{*{11}c}0.707 & 0.0 & 0.0 & -0.707j & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.707 & -0.707j & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & -0.707j & 0.707 & 0.0 & 0.0 & 0.0 & 0.0 & 0.0\\-0.707j & 0.0 & 0.0 & 0.707 & 0.0 & 0.0 & 0.0 & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & 0.707 & 0.0 & 0.0 & -0.707j\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & 0.707 & -0.707j & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & 0.0 & -0.707j & 0.707 & 0.0\\0.0 & 0.0 & 0.0 & 0.0 & -0.707j & 0.0 & 0.0 & 0.707\\\end{array}\right)\end{equation*}
In [10]:
qrot(np.pi/2,0)
Out[10]:
Quantum object: dims = [[2], [2]], shape = (2, 2), type = oper, isherm = False\begin{equation*}\left(\begin{array}{*{11}c}0.707 & -0.707j\\-0.707j & 0.707\\\end{array}\right)\end{equation*}
In [11]:
qrot(np.pi/2,np.pi/2, N=2, target = 0)
Out[11]:
Quantum object: dims = [[2, 2], [2, 2]], shape = (4, 4), type = oper, isherm = False\begin{equation*}\left(\begin{array}{*{11}c}0.707 & 0.0 & -0.707 & 0.0\\0.0 & 0.707 & 0.0 & -0.707\\0.707 & 0.0 & 0.707 & 0.0\\0.0 & 0.707 & 0.0 & 0.707\\\end{array}\right)\end{equation*}
In [ ]:
 

Comments

Popular posts from this blog

Final submission of the project

Summary GSoC 2019

Solving technical problems in using QuTiP solver for CircuitProcessor