# Entangling a register with another register using phase flips

 operation Algorithm() : Unit
{
using (register = Qubit[4])
{
within
{
PrepareUniformSuperposition(4,LittleEndian(register));
ApplyToEach(register[2],register);
}



I am having trouble with the ApplyToEach line. I am wondering if there is a way to entangle all the qubits in my register with another qubit, more broadly the 2(a) line 1. To my understanding I am satisfying the j term with uniform superposition. I am under the impression that when I apply the y-th qubit to my register I will entangle them. Maybe I am using the term entanglement wrong.

## 1 Answer

ApplyToEach operation applies an operation to each element of the array, you can't apply a qubit to each element of the array.

It sounds like you're trying to prepare a state in which all qubits will be entangled (the definition of entanglement is that you can't represent a state of the system as a tensor product of states of subsystems, so entangling each qubit with one of them will mean the whole system will be entangled). There are lots of ways to do this (since there are a lot of entangled states); if you don't care which state it is, the first thing that comes to mind is applying a Controlled Z gate to change the phase of the $$|1...1\rangle$$ state to $$-1$$:

Controlled Z(register[0..2], register[3]);


If you're looking to prepare a specific state, you might want to clarify this in the question.

To address the updated question:

• You need to allocate two registers, one to hold the values $$|j\rangle$$ and another to hold the values $$|y\rangle$$.
• The first line of 2(a) is almost satisfied with PrepareUniformSuperposition: after this the first register will hold $$\sum_j\frac{1}{\sqrt{N}}|j\rangle$$ and the second one $$|0\rangle$$.
• To finish preparing the state described in the first line of 2(a) you need to prepare the state $$|y\rangle$$ on the second register. That can be done by converting $$y$$ to binary and applying X gate to each qubit that corresponds to 1 bit.
• To implement the second line of 2(a), you need to write some extra code that compares $$j$$ with $$y$$ and "marks" the state (I'm assuming flips the phase? It's not clear from the screenshot) - this step will indeed entangle the qubits of the two registers. You might want to read more about reversible computing and quantum oracles to do this.
• When i intialize this second register will it be a completely different variable? Jun 30, 2020 at 11:06
• The second line is causing me a whole other layer of confusion. When researching other implementations it was left as an oracle to mark these states. I think a phase flip would be ideal, though this paper mentions this must be done in log(n) so iterating through the table and flipping each one wouldn't be ideal. Jun 30, 2020 at 11:09
• Yes, second register should be a separate variable (so you'd say "using (r1, r2) = (Qubit[4], Qubit[4])" when allocating them) Jun 30, 2020 at 16:51
• Thank you so much! this has cleared up a huge confusion ive had when implementing this alg. I was under the impression I needed to use tensor multiplication to achieve the initialized state. Jun 30, 2020 at 18:19
• @Mridul: Tensor products are how we use math to represent the joint state of two unentangled registers, but aren't represented in quantum programs directly. Rather, you can just allocate both registers as Mariia describes. Jun 30, 2020 at 21:41