def test_PrepareAndMeasureOnWFSim_QubitPlaceholders(): q1, q2 = QubitPlaceholder(), QubitPlaceholder() p = Program() params = p.declare("params", memory_type="REAL", memory_size=2) p.inst(RX(params[0], q1)) p.inst(RX(params[1], q2)) def make_memory_map(params): return {"params": params} ham = PauliSum([PauliTerm("Z", q1), PauliTerm("Z", q2)]) qubit_mapping = get_default_qubit_mapping(p) sim = WavefunctionSimulator() with local_qvm(): cost_fn = PrepareAndMeasureOnWFSim( p, make_memory_map, ham, sim, enable_logging=True, qubit_mapping=qubit_mapping, scalar_cost_function=False, ) out = cost_fn([np.pi, np.pi / 2], nshots=100) assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.1)) assert np.allclose(out, (-1, 0.1))
def test_PrepareAndMeasureOnQVM_QubitPlaceholders_nondiag_hamiltonian(): q1, q2, q3 = QubitPlaceholder(), QubitPlaceholder(), QubitPlaceholder() ham = PauliTerm("Y", q1) * PauliTerm("Z", q3) ham += PauliTerm("Y", q1) * PauliTerm("Z", q2, -0.3) ham += PauliTerm("Y", q1) * PauliTerm("X", q3, 2.0) params = [3.0, 0.4, 4.5] prepare_ansatz = Program() param_register = prepare_ansatz.declare("params", memory_type="REAL", memory_size=3) prepare_ansatz.inst(RX(param_register[0], q1)) prepare_ansatz.inst(RY(param_register[1], q2)) prepare_ansatz.inst(RY(param_register[2], q3)) def make_memory_map(params): return {"params": params} qubit_mapping = get_default_qubit_mapping(prepare_ansatz) qvm = get_qc("3q-qvm") with local_qvm(): cost_fn = PrepareAndMeasureOnQVM(prepare_ansatz, make_memory_map, qvm=qvm, hamiltonian=ham, scalar_cost_function=False, base_numshots=100, qubit_mapping=qubit_mapping) out = cost_fn(params, nshots=10) assert np.allclose(out, (0.346, 0.07), rtol=1.1)
def test_PrepareAndMeasureOnQVM_QubitPlaceholders(): q1, q2 = QubitPlaceholder(), QubitPlaceholder() prepare_ansatz = Program() param_register = prepare_ansatz.declare("params", memory_type="REAL", memory_size=2) prepare_ansatz.inst(RX(param_register[0], q1)) prepare_ansatz.inst(RX(param_register[1], q2)) def make_memory_map(params): return {"params": params} ham = PauliSum([PauliTerm("Z", q1), PauliTerm("Z", q2)]) qubit_mapping = get_default_qubit_mapping(prepare_ansatz) qvm = get_qc("2q-qvm") with local_qvm(): # qvm = proc.qvm cost_fn = PrepareAndMeasureOnQVM(prepare_ansatz, make_memory_map, qvm=qvm, hamiltonian=ham, enable_logging=True, scalar_cost_function=False, base_numshots=10, qubit_mapping=qubit_mapping) out = cost_fn([np.pi, np.pi / 2], nshots=10) assert np.allclose(cost_fn.log[0].fun, (-1.0, 0.1), rtol=1.1) assert np.allclose(out, (-1, 0.1), rtol=1.1)
def test_exponentiate_2(): # testing general 2-circuit q = QubitPlaceholder.register(8) generator = PauliTerm("Z", q[0], 1.0) * PauliTerm("Z", q[1], 1.0) para_prog = exponential_map(generator) prog = para_prog(1) result_prog = Program().inst(CNOT(q[0], q[1])).inst(RZ(2.0, q[1])).inst(CNOT(q[0], q[1])) mapping = get_default_qubit_mapping(prog) assert address_qubits(prog, mapping) == address_qubits(result_prog, mapping)
def test_vqe_on_WFSim_QubitPlaceholders(): qubit_mapping = get_default_qubit_mapping(prepare_ansatz) sim = WavefunctionSimulator() cost_fun = PrepareAndMeasureOnWFSim( prepare_ansatz=prepare_ansatz, make_memory_map=lambda p: {"params": p}, hamiltonian=hamiltonian, sim=sim, scalar_cost_function=True, qubit_mapping=qubit_mapping) out = scipy.optimize.minimize(cost_fun, p0, tol=1e-3, method="Cobyla") print(out) prog = address_qubits(prepare_ansatz, qubit_mapping=qubit_mapping) wf = sim.wavefunction(prog, {"params": out['x']}) print(wf.probabilities()) assert np.allclose(np.abs(wf.amplitudes**2), [0, 0, 0, 1], rtol=1.5, atol=0.01) assert np.allclose(out['fun'], -4) assert out['success']