# Extending op function definitions for the Qudit SiteType

**URL:** <https://itensor.discourse.group/t/extending-op-function-definitions-for-the-qudit-sitetype/289>\
**Category:** DMRG and Numerical Methods\
**Tags:** julia, mps\
**Created:** [July 27, 2022, 8:41am UTC](https://itensor.discourse.group/t/extending-op-function-definitions-for-the-qudit-sitetype/289 "2022-07-27T08:41:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuele](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@samuele](https://itensor.discourse.group/u/samuele)\
**Post date:** [July 27, 2022, 8:41am UTC](https://itensor.discourse.group/t/extending-op-function-definitions-for-the-qudit-sitetype/289/1 "2022-07-27T08:41:45Z")

</div>

Hello everyone,

I am new both to Julia and ITensor, so apologies for the possible trivial question. I want to implement a TEBD algorithm on an MPS for a transmon (qutrit) system. The Hamiltonian is composed of two terms, drift and control, \mathcal{H}=\mathcal{H}\_{\text{drift}}+\mathcal{H}\_{\text{ctrl}}: for my particular case I need to rotate \mathcal{H} in the control-diagonal basis.

I figured that the best way to do it would be to define a custom operators as in [this page](https://itensor.github.io/ITensors.jl/stable/examples/Physics.html) via the matrix representation of the rotated operator O (\hat{\mathcal{R}}^\dagger O\hat{\mathcal{R}}, with \hat{\mathcal{R}} the rotating matrix whose columns are the eigenvectors of \mathcal{H}\_{\text{ctrl}}) and then proceed as explained [here](https://itensor.github.io/ITensors.jl/stable/tutorials/MPSTimeEvolution.html).  
The issue is that while the extension of the `op` function for a `SiteType` `"S=1/2"` works as expected, if I try to change the class to `"Qudit"`, ITensor doesn’t recognize the new operator and throws `MethodError: no method matching _op(::OpName{:R}, ::SiteType{Qudit}; dim=(3,))`.  
Below an example using `Julia 1.7.3` and `ITensors v0.3.18`.

```julia
ITensors.op(::OpName"R",::SiteType"Qudit") =
 [1 0 0
  0 1 0
  0 0 1]
s = siteind("Qudit"; dim=3)
R = op("R",s)
@show R;

```

What am I missing here?  
As a follow up question, is there a way to apply the same transformation (multiplication by a rotation matrix) to the state (MPS)?

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [July 27, 2022, 8:26pm UTC](https://itensor.discourse.group/t/extending-op-function-definitions-for-the-qudit-sitetype/289/2 "2022-07-27T20:26:48Z")

</div>

Thanks for pointing out this issue. Actually what you’ve come across is a bug in the “Qudit” site type, so I’ll file an issue so that we will fix it. Fortunately the bug only affects this site type and only affect this kind of function overload, so as a workaround for now you can use a different “style” of overload of the `op` function, like this:

```auto
function ITensors.op(::OpName"R",::SiteType"Qudit",s::Index)
  M = [1 0 0
       0 1 0
       0 0 1]
   return ITensor(M,s',s)
 end

```

That version ought to work for your case & please let me know if it doesn’t.

---

<div class="post-metadata">

**Author:** ![miles](https://yyz2.discourse-cdn.com/free1/user_avatar/itensor.discourse.group/miles/32/6_2.png) [@miles](https://itensor.discourse.group/u/miles)\
**Post date:** [July 27, 2022, 8:49pm UTC](https://itensor.discourse.group/t/extending-op-function-definitions-for-the-qudit-sitetype/289/3 "2022-07-27T20:49:40Z")

</div>

Here’s the issue that I filed: [https://github.com/ITensor/ITensors.jl/issues/957](https://github.com/ITensor/ITensors.jl/issues/957)  
Matt and I will discuss the best way to fix it, but hopefully the workaround / alternate version I mentioned above will handle the case that you needed. 👍
