def check(project): for comp in project.compilations: res = project.get_result(comp) c = res['structure'] v = res['parameters'] assert matrix_distance_squared(c.matrix(v), project.get_target(comp)) < 1e-10
def test_openqasm_assemble_roundtrip(): s = ASSEMBLER_IBMOPENQASM.assemble(cdict) qc = QuantumCircuit.from_qasm_str(s) backend = qiskit.BasicAer.get_backend('unitary_simulator') job = qiskit.execute(qc, backend) U = job.result().get_unitary() BE = utils.endian_reverse(U) assert utils.matrix_distance_squared(BE, circ.matrix(parameters)) < 1e-10
def get_constant_depth_program(program, N, multistarts=24, optimizer_repeats=5): target_unitary = program.get_U() #get constant-depth circuit structure for N qubits circ_struct = make_MGC(N) #optimize parameters of circuit # use the multistart solver, may want to increase the number of starts for more qubits, but that will also be slower solv = multistart_solvers.MultiStart_Solver(multistarts) # set up some options opts = qsearch.Options() opts.target = target_unitary #opts.gateset = gateset opts.set_defaults(**standard_defaults) opts.set_smart_defaults(**standard_smart_defaults) # optimize the circuit structure (circ_struct) for target U # returns the calculated matrix and the vector of parameters dist = 1 # run a few times to make sure we find the correct solution for _ in range(optimizer_repeats): mat, vec = solv.solve_for_unitary(circ_struct, opts) dist_new = utils.matrix_distance_squared(mat, target_unitary) print(dist_new) if dist_new < dist: dist = dist_new if dist < 1e-10: break print(f'Got distance {dist}') #get final program result_dict = {} result_dict["structure"] = circ_struct result_dict["parameters"] = vec opts.assemblydict = assemblydict_ibmopenqasm out = opts.assembler.assemble(result_dict, opts) ibm_circ = qk.QuantumCircuit.from_qasm_str(out) prog = ibm_circ_to_program(ibm_circ) return prog
def test_assemble_ibmopenqasm(gate): parameters = np.random.uniform(size=(max(gate.num_inputs, 1),)) cdict = {"structure": gate, "parameters": parameters} s = ASSEMBLER_IBMOPENQASM.assemble(cdict) U = unitary_from_openqasm(s) assert utils.matrix_distance_squared(U, gate.matrix(parameters)) < 1e-10
U2 = utils.endian_reverse( U2) # switch from Qiskit endianess qsearch endianess # tell the optimizer what we are solving for opts = Options() opts.verbosity = 1 opts.stdout_enabled = True opts.target = unitaries.qft(32) #qc1.draw(output='mpl') #plt.show() circ, vec = qiskit_to_qsearch(qc1) print( f"Matrix distance of loaded circuit: {utils.matrix_distance_squared(circ.matrix(vec), unitaries.qft(32))}" ) print("Running some circuit verification checks...") circ.validate_structure() compare_gradient(circ) print("Passed checks!") print("Trying to re-optimize the circuit parameters...") for _ in range(100): U1, vec = solv.solve_for_unitary(circ, opts) dist = utils.matrix_distance_squared(U1, unitaries.qft(32)) if dist < 1e-10: break res = {'structure': circ, 'parameters': vec} print(dist) qasm = ASSEMBLER_IBMOPENQASM.assemble(res, opts) qc2 = qiskit.QuantumCircuit.from_qasm_str(qasm) #qc2.draw(output='mpl') #plt.show()
def test_rust_solver_qft3(): U = unitaries.qft(8) res = compile(U, LeastSquares_Jac_SolverNative()) circ = res['structure'] v = res['parameters'] assert utils.matrix_distance_squared(U, circ.matrix(v)) < 1e-10
def test_qft(): assert utils.matrix_distance_squared(qft_py, qft_rs) < 1e-14