> For the complete documentation index, see [llms.txt](https://chiwei955201314.gitbook.io/quantum/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://chiwei955201314.gitbook.io/quantum/deutsch-jozsa/oracle-and-query.md).

# oracle and query

#### Oracle&#x20;

我們視oracle為一個黑盒，並不在意裡面如何運作，通常oracle會有一個輸入函式，而我們能做的就是詢問這個oracle，而oracle會告訴我們答案。

#### Query Model

我們在說的時間複雜度，基本上都是基於query的次數，oracle內的複雜度我們並不需要去討論，因為他是個黑盒。

給定一個輸入函數 $$f(x)$$ ，而根據query後的結果，oracle一般來說又可分為兩種：

$$
\begin{aligned}
&|x,w\rangle\rightarrow|x,w\oplus f(x)\rangle\\
&|x\rangle\rightarrow(-1)^{f(x)}|x\rangle
\end{aligned}
$$

我們常稱第二種為phase oracle，因為他改變了量子態的機率幅的正負號，而事實上，兩者是等價的，以下給個小小的證明：

給定一個 $$f(x)=\left{0,1\right}^n\rightarrow\left{0,1\right}$$ 將第二個register設定為 $$|-\rangle$$，並query

$$
\begin{aligned}
|x,-\rangle\rightarrow|x,-\oplus f(x)\rangle
\end{aligned}
$$

開始展開化簡，因 $$|-\rangle = \frac{|0\rangle-|1\rangle}{\sqrt{2}}$$ ，得到以下結果

$$
|x,-\oplus f(x)\rangle = \frac{|x\rangle|0\oplus f(x)\rangle-|x\rangle|1\oplus f(x)\rangle}{\sqrt{2}}
$$

仔細觀察可以發現，當 $$f(x)=0$$ ，化簡後與原來並無區別

$$
\begin{aligned}
|x,-\oplus 0\rangle
&= \frac{|x\rangle|0\oplus 0\rangle-|x\rangle|1\oplus 0\rangle}{\sqrt{2}}\\
&=\frac{|x\rangle|0\rangle-|x\rangle|1\rangle}{\sqrt{2}}\\
&=|x,-\rangle
\end{aligned}
$$

但是當 $$f(x)=1$$ ，卻會相差一個負號

$$
\begin{aligned}
|x,-\oplus 1\rangle
&= \frac{|x\rangle|0\oplus 1\rangle-|x\rangle|1\oplus 1\rangle}{\sqrt{2}}\\
&=\frac{|x\rangle|1\rangle-|x\rangle|0\rangle}{\sqrt{2}}\\
&=-|x,-\rangle
\end{aligned}
$$

透過這個證明，我們可以了解到兩種oracle的等價，也就是：

$$
|x,-\oplus f(x)\rangle = (-1)^{f(x)}|x\rangle|-\rangle
$$


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://chiwei955201314.gitbook.io/quantum/deutsch-jozsa/oracle-and-query.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
