X_MAX = 2**NUM_QUBITS - 1
if EXP_RATE > 0:
AMPLITUDE = get_good_states_amplitude(
X_MAX - (X1
- X0), X_MAX, EXP_RATE, NUM_QUBITS
)
else:
AMPLITUDE = get_good_states_amplitude(0, X1
- X0, EXP_RATE, NUM_QUBITS)
print(AMPLITUDE)
@qperm
def oracle_comp(x: Const[QNum], res: QBit):
if EXP_RATE > 0:
res ^= x >= X_MAX - (X1
- X0)
else:
res ^= x <= (X1
- X0)
@qfunc
def main(x: Output[QNum]):
allocate(NUM_QUBITS, x)
exact_amplitude_amplification(
amplitude=AMPLITUDE,
oracle=lambda _x: phase_oracle(oracle_comp, _x),
space_transform=lambda _x: prepare_exponential_state(-EXP_RATE, _x),
packed_qvars=x,
)
# shift to the wanted domain
if EXP_RATE > 0:
x += -(X_MAX
- X1)
else:
x += X0
qmod = create_model(main, execution_preferences=execution_preferences)
qprog = synthesize(qmod)
show(qprog)
res = execute(qprog).get_sample_result()
x, measured_amps = parse_res(res, plot=False)
# compare to expected amplitudes
grid = np.arange(X0, X1 + 1)
expected_amps = np.sqrt(np.exp(EXP_RATE * grid))
expected_amps /= np.linalg.norm(expected_amps)
plt.scatter(grid, expected_amps, marker="+", s=100, label="expected")
plt.scatter(x, measured_amps, label="measured")
plt.xlabel("x")
plt.ylabel("amplitude")
plt.legend()
plt.show()