0
$\begingroup$

I apply Grover's on an eigh qubits circuit I just want to amplify the states whose qubits 6 and 7 are |1> The following test works (the state 11011001 is correctly amplified)

good_state=[6,7]
# Test
oracle = Statevector.from_label('11011001')
...

but is it possible to replace the label '11011001' by something like '11<any 6 bits>'? An more generally by '<n times 1><(8-n any bits)>'? More precisely, here is an example. After some manipulations, the state vector is $ \frac{\sqrt{2}}{4} |00010101\rangle+\frac{\sqrt{2}}{4} |00101010\rangle+\frac{\sqrt{2}}{4} |01010110\rangle+\frac{\sqrt{2}}{4} |01101001\rangle+\frac{\sqrt{2}}{4} |10011010\rangle+\frac{\sqrt{2}}{4} |10100101\rangle+\frac{\sqrt{2}}{4} |11011001\rangle+\frac{\sqrt{2}}{4} |11100110\rangle $ Ideally, in that case, the "label" should be something like '11011001 OR 11100110'.

---- 2023-06-29 Actually I simplified the problem. I now just want to amplify the states whose binary strings in the state vector have the leftmost bit equal to 1.

$\endgroup$

2 Answers 2

1
$\begingroup$

If you want your oracle to mark any bitstring in the form $|11******\rangle$, you can use

oracle = 8 * Statevector.from_label('11++++++')

Basically, this statevector is a linear combination of all the states in this form.

In general, if bitstrings are of length $N$, and you want your oracle to mark bitstrings with $K$ ones on the leftmost bits, your statevector would be:

N = 8
K = 2
oracle = np.sqrt(2 ** (N - K)) * Statevector.from_label('1'*K + '+'*(N - K))

To check its validity, get the generated circuit for this oracle and apply it to an equal superposition:

circ = problem.grover_operator.oracle.decompose()

Statevector.from_label('+'*N).evolve(circ).draw('latex', max_size=(2 ** N))

Check that all the states that should be marked have a negative sign in the output.

Note: if you want to mark more than a few states, you should consider more efficient way to build the oracle. For example, this circuit will mark bitstrings with $K$ ones on the leftmost bits

from qiskit import QuantumCircuit

oracle = QuantumCircuit(N)
oracle.h(N - 1)
oracle.mcx(list(range(N - K, N - 1)), N - 1)
oracle.h(N - 1)
$\endgroup$
5
  • $\begingroup$ it was just an eight qubits example. But the number of qubits may be different and, also, I don't know in advance the exact interesting binary strings. I just know the n leftmost bits are 1. $\endgroup$ Jun 29, 2023 at 18:21
  • $\begingroup$ I updated my answer $\endgroup$ Jun 30, 2023 at 7:02
  • $\begingroup$ oracle.mcx(list(range(N - K, N - 1)), N - 1)? What about K=1? $\endgroup$ Jul 1, 2023 at 7:57
  • $\begingroup$ To mark all $|1*******\rangle$ states your oracle can be just one Z-gate. That is, oracle.z(N - 1). However, I don't think Grover algorithm will work in this case (see here). $\endgroup$ Jul 1, 2023 at 8:12
  • $\begingroup$ Indeed. Workaround in my case: ensure that not half the possible strings are solutions. $\endgroup$ Jul 1, 2023 at 8:33
0
$\begingroup$

I think that the simplest solution is to randomly generate a bit string of length 8-n and then use string concatenation to make up your final label for creating the oracle Statevector:

import random
from qiskit.quantum_info import Statevector

tot = 8
n = 2
m = tot - n

rand_int = random.randint(0, 2**m-1)
rand_bitstr = bin(rand_int)[2:].zfill(m)

label = n * '1' + rand_bitstr
oracle = Statevector.from_label(label)
$\endgroup$
1
  • $\begingroup$ My question was not very clear. I added an example. $\endgroup$ Jun 27, 2023 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.