diff --git a/qsearch/leap_compiler.py b/qsearch/leap_compiler.py index d5db526..f96fd81 100644 --- a/qsearch/leap_compiler.py +++ b/qsearch/leap_compiler.py @@ -146,7 +146,8 @@ def compile(self, options=Options()): logger.logprint("This gateset has no branching factor so only an initial optimization will be run.") root = initial_layer result = options.solver.solve_for_unitary(options.backend.prepare_circuit(root, options), options) - return (root, result[1]) + value = options.eval_func(U, result[0]) + return ((root, result[1]), value, 0) parallel = options.parallelizer(options) # TODO move these print statements somewhere else diff --git a/tests/test_stateprep.py b/tests/test_stateprep.py index 07bdbec..779040b 100644 --- a/tests/test_stateprep.py +++ b/tests/test_stateprep.py @@ -20,24 +20,25 @@ def simulate_statevector(qc): return np.array(result.get_statevector(qc)) - -def qsearch_stateprep(project, target_state): +def qsearch_stateprep(project, target_state, leap=False): stateprep_options = qsearch.Options(target_state=target_state,smart_defaults=qsearch.defaults.stateprep_smart_defaults) project.add_compilation("stateprep",stateprep_options.target,options=stateprep_options) + if leap: + project['compiler_class'] = leap_compiler.LeapCompiler project.run() qiskit_code = project.assemble("stateprep", assembler=qsearch.assemblers.ASSEMBLER_QISKIT) locals = {} exec(qiskit_code, globals(), locals) return locals['qc'] -STATES = map(np.array, ( +STATES = list(map(np.array, ( [1/np.sqrt(2),-1/np.sqrt(2)], [0.5,0.5,0.5,0,0,0,0,0.5], [0,0.5,0.5j,0,-0.5,0,0,0,-0.5j,0,0,0,0,0,0,0] -)) +))) @pytest.mark.skipif(qiskit is None, reason="Qiskit is not installed") -@pytest.mark.parametrize("target_state", STATES, ids=lambda state: repr(state[:5])) +@pytest.mark.parametrize("target_state", STATES, ids=lambda state: repr(state)) def test_stateprep_roundtrip(project, target_state): qc = qsearch_stateprep(project, utils.endian_reverse(target_state)) output_state = simulate_statevector(qc) @@ -48,3 +49,17 @@ def test_stateprep_roundtrip(project, target_state): qc2.id(i) output_state_ibm = simulate_statevector(qc2) assert np.isclose(np.abs(np.vdot(output_state,output_state_ibm)),1) + + +@pytest.mark.skipif(qiskit is None, reason="Qiskit is not installed") +@pytest.mark.parametrize("target_state", STATES, ids=lambda state: repr(state)) +def test_stateprep_leap_roundtrip(project, target_state): + qc = qsearch_stateprep(project, utils.endian_reverse(target_state), leap=True) + output_state = simulate_statevector(qc) + num_qubits = int(np.log2(target_state.shape[0])) + qc2 = QuantumCircuit(num_qubits) + qc2.initialize(target_state, list(range(num_qubits))) + for i in range(num_qubits): + qc2.id(i) + output_state_ibm = simulate_statevector(qc2) + assert np.isclose(np.abs(np.vdot(output_state,output_state_ibm)),1)