Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Fix a bug in LEAP and add tests for LEAP stateprep
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanhs committed Mar 16, 2021
1 parent 830269c commit c38ff9c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
3 changes: 2 additions & 1 deletion qsearch/leap_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions tests/test_stateprep.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit c38ff9c

Please sign in to comment.