### Demo for user defined gates

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]:

In [6]:

```
props[1] # qubit 1 controlls qutbi 0
```

Out[6]:

In [7]:

```
props[2] # T gate acts on qubit 1
```

Out[7]:

## molmer sorensen gate and qubit rotation gate¶

In [8]:

```
molmer_sorensen(np.pi/2, targets=[0, 1])
```

Out[8]:

In [9]:

```
molmer_sorensen(np.pi/2, N=3, targets=[1,2])
```

Out[9]:

In [10]:

```
qrot(np.pi/2,0)
```

Out[10]:

In [11]:

```
qrot(np.pi/2,np.pi/2, N=2, target = 0)
```

Out[11]:

In [ ]:

```
```

## Comments

## Post a Comment