0

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.

2 Answers2

1

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)

Egretta.Thula
  • 11,986
  • 1
  • 13
  • 34
0

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)

SimoneGasperini
  • 1,634
  • 1
  • 3
  • 18