# Apply quantum channel to MPO

**URL:** <https://itensor.discourse.group/t/apply-quantum-channel-to-mpo/295>\
**Category:** ITensor Julia Questions\
**Tags:** julia, mpo\
**Created:** [July 28, 2022, 12:05am UTC](https://itensor.discourse.group/t/apply-quantum-channel-to-mpo/295 "2022-07-28T00:05:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nticea](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@nticea](https://itensor.discourse.group/u/nticea)\
**Post date:** [July 28, 2022, 12:05am UTC](https://itensor.discourse.group/t/apply-quantum-channel-to-mpo/295/1 "2022-07-28T00:05:00Z")

</div>

Hi, apologies for the spate of questions. This will hopefully be the last for a while.

My goal is to apply a quantum channel to a density matrix represented as an MPO. Now, ordinarily, I would simply use apply() here. However, in this case the number of input and output indices is different. For example, you could think of this as a (non-unitary) quantum channel that ties together 4 sites into 1. Here is my best guess for how something like that might work:

```auto
function apply_channel(ρ::MPO, Ψ::ITensor)
    ρ̃ = copy(ρ)

    # extract the common indices where we will be applying the channel 
    common_inds = findall(x -> hascommoninds(Ψ, ρ[x]), collect(1:length(ρ)))

    # Apply the channel 
    wf = ρ̃[common_inds[1]] * Ψ
    for v in common_inds[2:end]
        wf *= ρ̃[v] 
    end

    # Update the MPO
    new_data = vcat(ρ̃[1:common_inds[1]-1], wf, ρ̃[common_inds[end]+1:length(ρ̃)])
    ρ̃.data = new_data

    return ρ̃
end

```

I have no real reason to believe that this is wrong, save for the fact that my code as a whole is not working and I am trying to pinpoint the location of my bug. Please let me know if you have any thoughts! And thank you again for all the help.

---

<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 28, 2022, 11:00am UTC](https://itensor.discourse.group/t/apply-quantum-channel-to-mpo/295/2 "2022-07-28T11:00:47Z")

</div>

What is the error message you are getting?

---

<div class="post-metadata">

**Author:** ![nticea](https://avatars.discourse-cdn.com/v4/letter/n/eb8c5e/32.png) [@nticea](https://itensor.discourse.group/u/nticea)\
**Post date:** [July 28, 2022, 3:34pm UTC](https://itensor.discourse.group/t/apply-quantum-channel-to-mpo/295/3 "2022-07-28T15:34:37Z")

</div>

Hi Miles,

This part of the code does not actually throw an error message – I am just not getting the results I expect from the script as a whole, and was wondering whether that might be because I am making some mistake in applying the quantum channel to the MPO. But if this looks fine to you, then I will have to look for my bug elsewhere…

Thanks so much! (Also happy to provide more information here or via email if you think that would be helpful)

---

<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:** [August 1, 2022, 2:24am UTC](https://itensor.discourse.group/t/apply-quantum-channel-to-mpo/295/4 "2022-08-01T02:24:45Z")

</div>

I see - however, I’m not sure if the code you posted has a bug or not, but the question wasn’t really specific enough for me to know what might be the issue you were asking about. If you have a more specific or narrowly tailored question, please ask, otherwise I encourage you to try out various parts of your code on small examples where you know what the correct result should be and/or print out intermediate values of your code as it is running to verify if it’s working correctly.

Best,  
Miles
