Skip to content

Commit

Permalink
Katas UI string review - Oracles (#1742)
Browse files Browse the repository at this point in the history
Co-authored-by: Bill Ticehurst <billti@hotmail.com>
  • Loading branch information
SoniaLopezBravo and billti authored Jul 18, 2024
1 parent 7d4a7bd commit 3e770e7
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 65 deletions.
2 changes: 1 addition & 1 deletion katas/content/oracles/bit_pattern_challenge/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ represented by `pattern`.
<br/>
<details>
<summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>
This is a phase oracle, because we are changing the phase of the input state $\ket{x}$ based on the value of the function $f(x)$.
This is a phase oracle, because you're changing the phase of the input state $\ket{x}$ based on the value of the function $f(x)$.
</details>

<br/>
Expand Down
2 changes: 1 addition & 1 deletion katas/content/oracles/bit_pattern_oracle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ represented by `pattern`.
<br/>
<details>
<summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>
This is a marking oracle, because we are flipping the state of the target qubit $\ket{y}$ based on the state of the input $\ket{x}$.
This is a marking oracle, because you're flipping the state of the target qubit $\ket{y}$ based on the state of the input $\ket{x}$.
</details>

<br/>
Expand Down
4 changes: 2 additions & 2 deletions katas/content/oracles/classical_oracles/solution.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
The binary notation of $7$ is $111_2$. This means that our solution should return `true` if and only if each bit of the input is `true`.
The binary notation of $7$ is $111_2$. This means that your solution should return `true` if and only if each bit of the input is `true`.

Notice that in this problem the solution doesn't depend on whether the bit string is converted into an integer using
big endian or little endian notation. In general this is an important distinction that you need to take into account.
big endian or little endian notation. In general, this is an important distinction that you need to take into account.

@[solution]({
"id": "oracles__classical_oracles_solution",
Expand Down
70 changes: 35 additions & 35 deletions katas/content/oracles/index.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion katas/content/oracles/kth_bit_oracle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ Flip the sign of the input state $\ket{x}$ if the $k$-th bit of $x$ is $1$.
<br/>
<details>
<summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>
This is a phase oracle, because we are changing the phase of the input state $\ket{x}$ based on the value of the function $f(x)$.
This is a phase oracle, because you're changing the phase of the input state $\ket{x}$ based on the value of the function $f(x)$.
</details>
4 changes: 2 additions & 2 deletions katas/content/oracles/kth_bit_oracle/solution.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Since the effect of this oracle depends only on the value of the $k$-th qubit, we can ignore the rest of the qubits and focus on just `x[k]`.
We need to flip the phase of the qubit if it is in the $\ket{1}$ state and leave it unchanged otherwise - this is exactly the effect of the $Z$ gate.
Since the effect of this oracle depends only on the value of the $k$-th qubit, you can ignore the rest of the qubits and focus on just `x[k]`.
You need to flip the phase of the qubit if it is in the $\ket{1}$ state and leave it unchanged otherwise - this is exactly the effect of the $Z$ gate.

@[solution]({
"id": "oracles__kth_bit_oracle_solution",
Expand Down
2 changes: 1 addition & 1 deletion katas/content/oracles/marking_oracle_as_phase/solution.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
As we just saw, we can allocate an additional qubit in the $\ket{-}$ state and use it as the target for our marking oracle.
As you just saw, you can allocate an additional qubit in the $\ket{-}$ state and use it as the target for our marking oracle.
This will kick back the $-1$ relative phase for the basis states $\ket{x}$ of the input register for which $f(x) = 1$.

@[solution]({
Expand Down
8 changes: 4 additions & 4 deletions katas/content/oracles/marking_oracle_seven/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ Now let's again apply this oracle to a superposition state $\ket{\alpha}$ such t
$$\ket{\alpha} = \frac{1}{\sqrt{2}}\big(\ket{110} + \ket{111}\big)\ket{0} =
\ket{11} \otimes \frac{1}{\sqrt{2}} \big(\ket{0} + \ket{1}\big) \otimes \ket{0} = \ket{11+} \ket{0}$$

Let's consider how our operator $U_{7,mark}$ acts on this state.
Let's consider how the operator $U_{7,mark}$ acts on this state.

> Recall that oracles are linear operators, thus they can be applied to each term individually.
$$U_{7,mark} \ket{\alpha} = \frac{1}{\sqrt{2}} \big(U_{7,mark}\ket{110} \ket{0} + U_{7,mark}\ket{111} \ket{0}\big) =$$

$$= \frac{1}{\sqrt{2}} \big(\ket{110} \ket{0} + \ket{111} \ket{1}\big) := \ket{\epsilon}$$

Was our input state modified during this operation? Let's simplify the resulting state $\ket{\epsilon}$:
Was your input state modified during this operation? Let's simplify the resulting state $\ket{\epsilon}$:

$$\ket{\epsilon} = \frac{1}{\sqrt{2}} \big(\ket{110} \ket{0} + \ket{111} \ket{1}\big) = \ket{11} \otimes \frac{1}{\sqrt{2}} \big(\ket{0} \ket{0} + \ket{1} \ket{1}\big) =$$

$$= \ket{11} \otimes \frac{1}{\sqrt{2}} \big(\ket{00} + \ket{11}\big) = \ket{11} \otimes \ket{\Phi^+} = \ket{11\Phi^+}$$

We have entangled the states of qubits $\ket{x}$ and $\ket{y}$! This is a common occurrence for marking oracles when the input is a superposition of basis states: after applying the oracle, the input $\ket{x}$ will often become entangled with $\ket{y}$. Thus, while applying the marking oracle to a basis state will leave the input array unchanged, applying the marking oracle to a superposition state will change the state of both the input array and the output qubit.
You have entangled the states of qubits $\ket{x}$ and $\ket{y}$! This is a common occurrence for marking oracles when the input is a superposition of basis states: after applying the oracle, the input $\ket{x}$ will often become entangled with $\ket{y}$. Thus, while applying the marking oracle to a basis state will leave the input array unchanged, applying the marking oracle to a superposition state will change the state of both the input array and the output qubit.

>As an exercise, what entangled state would we get in the previous example if $\ket{y} = \ket{1}$ instead of $\ket{y} = \ket{0}$?
>As an exercise, what entangled state would you get in the previous example if $\ket{y} = \ket{1}$ instead of $\ket{y} = \ket{0}$?
>
> <details>
> <summary><b>Answer</b></summary>
Expand Down
6 changes: 3 additions & 3 deletions katas/content/oracles/meeting_oracle/index.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Suppose that you would like to schedule a meeting with your co-worker Jasmine.
Suppose that you'd like to schedule a meeting with your co-worker Jasmine.
You both work five day workweeks, and $\ket{x}$ and $\ket{jasmine}$ are 5-bit states representing your and Jasmine's schedules.
The schedules are indicators of a person being busy on that day: a $1$ bit means that person is busy on that day, and $0$ means they're free for a meeting that day. Implement a function that determines if you and Jasmine can schedule a meeting during the week, i.e., whether there is a day when both schedules have a $0$ simultaneously.
The schedules are indicators of a person being busy on that day: a $1$ bit means that person is busy on that day, and $0$ means they're free for a meeting that day. Implement a function that determines if you and Jasmine can schedule a meeting during the week, that is, whether there is a day when both schedules have a $0$ simultaneously.

**Inputs:**

Expand All @@ -22,5 +22,5 @@ Flip the state of $\ket{y}$ if you and Jasmine are both free on the same day for
<br/>
<details>
<summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>
This is a marking oracle, because we are flipping the state of the target qubit $\ket{y}$ based on the state of the inputs $\ket{x}$ and $\ket{jasmine}$. Notice that even though we do not have the typical single-input-register situation that we saw earlier, this is still a marking oracle.
This is a marking oracle, because you're flipping the state of the target qubit $\ket{y}$ based on the state of the inputs $\ket{x}$ and $\ket{jasmine}$. Notice that even though you don't have the typical single-input-register situation that we saw earlier, this is still a marking oracle.
</details>
6 changes: 3 additions & 3 deletions katas/content/oracles/meeting_oracle/solution.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
In this problem we'll need to allocate extra qubits, one per each day of the week, to serve as temporary variables.
In this problem you'll need to allocate extra qubits, one per each day of the week, to serve as temporary variables.
Each of these variables will track whether both people are free on that day, that is, whether both input qubit arrays
have bit $0$ in the corresponding position. We can compute each of these variables using a $CCNOT$ gate after flipping
have bit $0$ in the corresponding position. You can compute each of these variables using a $CCNOT$ gate after flipping
the states of both qubits using $X$ gates.

After this, we can reuse the marking OR oracle to check whether any of the days are in $\ket{1}$ state.
After this, you can reuse the marking OR oracle to check whether any of the days are in $\ket{1}$ state.

When you allocate qubits for temporary use during a larger computation, you need to make sure they are returned to the $\ket{0}$
state before being released. Unlike your previous experience allocating qubits in demos, though, this time you cannot
Expand Down
4 changes: 2 additions & 2 deletions katas/content/oracles/or_but_kth_oracle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

**Goal:**

Flip the sign of the basis state $\ket{x}$ if any of the bits of $x$ (not considering the $k$-th bit) are $1$ in the input register. In other words, the input register with the $k$-th qubit excluded should not be in the all zero state to flip the sign of the input register. The state of the $k$-th qubit does not affect the result.
Flip the sign of the basis state $\ket{x}$ if any of the bits of $x$ (not considering the $k$-th bit) are $1$ in the input register. In other words, the input register with the $k$-th qubit excluded shouldn't be in the all zero state to flip the sign of the input register. The state of the $k$-th qubit does not affect the result.

*Feel free to explore implementing this operation with or without auxiliary qubits.*

Expand All @@ -16,7 +16,7 @@ Flip the sign of the basis state $\ket{x}$ if any of the bits of $x$ (not consid

<details>
<summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>
This is a phase oracle, because we are changing the phase of the input state $\ket{x}$ based on the value of the function $f(x)$.
This is a phase oracle, because you're changing the phase of the input state $\ket{x}$ based on the value of the function $f(x)$.
</details>

<details>
Expand Down
2 changes: 1 addition & 1 deletion katas/content/oracles/or_oracle/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ except for $\ket{00...0}$ (the all zero state).
<br/>
<details>
<summary><b>Before implementing this oracle, answer the question: are you implementing a marking or a phase oracle?</b></summary>
This is a marking oracle, because we are flipping the state of the target qubit $\ket{y}$ based on the state of the input $\ket{x}$.
This is a marking oracle, because you're flipping the state of the target qubit $\ket{y}$ based on the state of the input $\ket{x}$.
</details>

<br/>
Expand Down
4 changes: 2 additions & 2 deletions katas/content/oracles/or_oracle/solution.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
What if you needed to flip the state of the target qubit only if the input register was in the $\ket{0...0}$ state?
What if you needed to flip the state of the target qubit only if the input register is in the $\ket{0...0}$ state?
In that case, you could use `ApplyControlledOnInt` with the control pattern $0$ that corresponds to all control qubits in $\ket{0}$ state.

In this problem, you need to separate the $\ket{0...0}$ basis state from all others, but with the opposite effect on the target qubit: instead of flipping it for this input state, you need to flip it for all other input states.
Or, you can think of it as first flipping the state of the target qubit for all states, and then un-flipping it (or flipping it again) for just this basis state. You can do this by applying the $X$ gate before or after `ApplyControlledOnInt`.

Notice that you can modify the state of the input register during your computations (this is what `ApplyControlledOnInt` function does under the hood). However, it is essential to undo those modifications ("uncompute" the changes), except the final one, so that the oracle will preserve the input if it is a basis state.
Notice that you can modify the state of the input register during your computations (this is what `ApplyControlledOnInt` function does under the hood). However, it's essential to undo those modifications ("uncompute" the changes), except the final one, so that the oracle will preserve the input if it's a basis state.

@[solution]({
"id": "oracles__or_oracle_solution",
Expand Down
14 changes: 7 additions & 7 deletions katas/content/oracles/phase_oracle_seven/solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ Consider how this oracle acts on two basis states:
$$U_{7,phase} \ket{111} = -\ket{111}$$
$$U_{7,phase} \ket{110} = \ket{110}$$

You can see that $U_{7,phase}$ does not change the input if it's a basis state (other than adding a global phase), and $U_{7,phase}$ does not change the norm of the state ($U_{7,phase}$ is a unitary operator).
You can see that $U_{7,phase}$ doesn't change the input if it's a basis state (other than adding a global phase), and $U_{7,phase}$ doesn't change the norm of the state ($U_{7,phase}$ is a unitary operator).

However, if we applied this oracle to a superposition state instead, what will that look like?
However, if you applied this oracle to a superposition state instead, what will that look like?

Suppose that $\ket{\beta}$ is an equal superposition of the states $\ket{110}$ and $\ket{111}$:
$$\ket{\beta} = \frac{1}{\sqrt{2}} \big(\ket{110} + \ket{111}\big) = \ket{11} \otimes \frac{1}{\sqrt{2}} \big(\ket{0} + \ket{1}\big) = \ket{11} \otimes \ket{+} = \ket{11+}$$

Let's consider how our operator $U_{7,phase}$ acts on this state:
Let's consider how the operator $U_{7,phase}$ acts on this state:

$$U_{7,phase} \ket{\beta} = U_{7,phase} \Big[\frac{1}{\sqrt{2}} \big(\ket{110} + \ket{111}\big)\Big] =$$

$$= \frac{1}{\sqrt{2}} \big(U_{7,phase} \ket{110} + U_{7,phase} \ket{111}\big) =$$

$$= \frac{1}{\sqrt{2}} \big(\ket{110} - \ket{111}\big) := \ket{\gamma}$$

Was our input state modified during this operation? Let's simplify $\ket{\gamma}$:
Was your input state modified during this operation? Let's simplify $\ket{\gamma}$:

$$\ket{\gamma} = \frac{1}{\sqrt{2}} \big(\ket{110} - \ket{111}\big) =$$

$$= \ket{11} \otimes \frac{1}{\sqrt{2}} \big(\ket{0} - \ket{1}\big) =$$

$$= \ket{11} \otimes \ket{-} = \ket{11-} \neq \ket{\beta}$$

Here we see that the oracle modifies the input, if the input state was a *superposition* of the basis states, as a phase oracle will only modify the sign of the basis states. Thus when a superposition state is provided as input to an oracle, the input state can be modified via the application of the quantum oracle.
Here you see that the oracle modifies the input, if the input state is a *superposition* of the basis states, as a phase oracle will only modify the sign of the basis states. Thus when a superposition state is provided as input to an oracle, the input state can be modified via the application of the quantum oracle.

> It is also worth noting that while the oracle modified the input when provided a superposition state, it did *not* modify the norm of that state. As an exercise, you can verify this yourself by taking the norm of $\ket{\beta}$ and $\ket{\gamma}$, which both will result in a value of $1$.
> It's also worth noting that while the oracle modified the input when provided a superposition state, it did *not* modify the norm of that state. As an exercise, you can verify this yourself by taking the norm of $\ket{\beta}$ and $\ket{\gamma}$, which both will result in a value of $1$.
>
> As another exercise, consider how you could distinguish between the input and output state programmatically? Is there an operation that you could apply to the initial state $\ket{\beta}$ and the final state $\ket{\gamma}$ to show that the two states are not equivalent through measurement? As a hint, think about how you could convert the superposition states $\ket{\beta}$ and $\ket{\gamma}$ into the basis states.
> As another exercise, consider how you could distinguish between the input and output state programmatically? Is there an operation that you could apply to the initial state $\ket{\beta}$ and the final state $\ket{\gamma}$ to show that the two states aren't equivalent through measurement? As a hint, think about how you could convert the superposition states $\ket{\beta}$ and $\ket{\gamma}$ into the basis states.

0 comments on commit 3e770e7

Please sign in to comment.