# How to define Q-operator in Quantum Amplitude Estimation

I'm trying to implement a circuit for Quantum Amplitude Estimation in Qiskit using elementary gates.

I have created the circuit that represent my algorithm $$A$$ but now from the theory I know that I have to create the Q-operator defined as: $$Q = A S_0 A^{-1} S_{\psi_{0}}$$ , where $$S_0$$ and $$S_{\psi_{0}}$$ are two reflections.

How can I implement these two reflections in the circuit using Qiskit gates?

UPDATE
I built a quantum circuit for reproducing an algorithm $$A$$ for computing expected value of a random variable, given by:

1. Load a random variable X as a quantum state

$$L|0\rangle_n = |\psi\rangle_n = \sum_{i=0}^{2^n - 1}\sqrt{p_i} |i\rangle_n \ \ \ such \ that \ \sum_{i=0}^{2^n - 1}p_i = 1$$

1. Create an operator for the encoding

$$F|i\rangle_n |0\rangle = \sqrt{1 - f(i)} |i\rangle_n |0\rangle + \sqrt{f(i)} |i\rangle_n |1\rangle$$

So my algorithm $$A$$ is given by the final state:

$$F (L|0\rangle_n)|0\rangle = F|\psi\rangle_n|0\rangle = \sum_{i=0}^{2^n-1} \sqrt{1 - f(i)} \sqrt{p_i} |i\rangle_n |0\rangle + \sum_{i=0}^{2^n-1} \sqrt{f(i)} \sqrt{p_i} |i\rangle_n |1\rangle$$
I used 3 qubits for loading distribution and one ancilla qubit; so my Qiskit circuit is the following

From this I would create $$Q$$ operator for Amplitude Estimation. How can I procede?

Check out qiskit.aqua.algorithms.amplitude_estimators.q_factory.QFactory which constructs $$Q$$ if you provide it with $$A$$. You can use the i_objective argument to specify the "good" state in $$S_{\Psi_0}$$.

The $$S_0$$ operation is flips the sign of the $$|0\rangle$$ state and leaves all the others in place. This can be implemented with a multi-controlled Z gate with X gates around the target gate, so it applies a -1 factor to $$|0\rangle$$ and not $$|1\rangle$$. In Qiskit, you can do that with the QuantumCircuit.mcx method and Hadamard gates around that (since there's no mcz method and HXH = Z):

from qiskit import QuantumCircuit
s0 = QuantumCircuit(n)
s0.x(n - 1)
s0.h(n - 1)
s0.mcx(list(range(n - 1)), n - 1)
s0.h(n - 1)
s0.x(n - 1)


The $$S_{\Psi_0}$$ operation, called the oracle in Grover's algorithm, applies a -1 factor to the "good" qubit states. This requires additional information, how do you determine if a state is "good" or "bad" in your scenario?

As an example: In many optimization examples we define the operator $$A$$ as $$A|0\rangle^{\otimes (n + 1)} = \sqrt{1 - a} |\psi_0\rangle|0\rangle + \sqrt{a} |\psi_1\rangle|1\rangle$$ for $$n$$-qubit states $$|\psi_{0,1}\rangle$$. There we define good states by the last qubit begin in state $$|1\rangle$$ and hence the circuit for $$S_{\Psi_0}$$ is just a $$Z$$ gate on the last qubit:

s_psi0 = QuantumCircuit(n + 1)
s_psi0.z(n)


In the amplitude estimation algorithm, the QFactory class (full import qiskit.aqua.algorithms.amplitude_estimators.q_factory.QFactory) is used, which constructs $$Q$$ if you provide it with $$A$$. There it assumes that the good state can be specified by the a single qubit begin in state $$|1\rangle$$. The index of this qubit is specified via i_objective (per default the last qubit index is used).

The above example, where the "good" state is specified by the last qubit being in state $$|1\rangle$$ would therefore be

from qiskit.aqua.algorithms.amplitude_estimators.q_factory import QFactory
q = QFactory(your_a_factory)

• Could you please reference some works where they have this additional qubit? Thank you! Sep 18 '20 at 15:51
• Hi, I try to modify my question in order to make it more precise. I hope you could help me Sep 29 '20 at 13:14