# Output energy tallies. We first recompute these because some entries seem to # be multiply listed. if cl_args.verbose >= 2: try: tallies = answer["num_occurrences"] except KeyError: tallies = [1] * len(energies) new_energy_tallies = {} for i in range(len(energies)): e = float(energies[i]) t = int(tallies[i]) try: new_energy_tallies[e] += t except KeyError: new_energy_tallies[e] = t new_energies = sorted(new_energy_tallies.keys()) min_energy_possible = -sum( [abs(w) for w in physical_ising.weights] + [abs(s) for s in physical_ising.strengths.values()]) sys.stderr.write("Energy histogram (theoretical minimum = %.4f):\n\n" % min_energy_possible) sys.stderr.write(" Energy Tally\n") sys.stderr.write(" ---------- ------\n") for e in new_energies: sys.stderr.write(" %10.4f %6d\n" % (e, new_energy_tallies[e])) sys.stderr.write("\n") # Output the solution to the standard output device. qmasm.output_solution(id2solution, num_occurrences, cl_args.values)
) num_value = { pre_simple_sym_map.to_number(sym): value for sym, value in logical_ising.known_values.items() } energy = pre_simple_problem.energy_from_known_values(num_value) physical_ising = logical_ising.add_fake_physical_fields() answer = { "solutions": [[]], "num_occurrences": [cl_args.samples], "energies": [energy] } solutions = qmasm.Solutions(answer, physical_ising, cl_args.verbose >= 2) show_asserts = cl_args.verbose >= 2 or cl_args.show in ["best", "all"] qmasm.output_solution(solutions, cl_args.values, cl_args.verbose, show_asserts) sys.exit(0) else: # The program was originally non-empty but was reduced to a empty # program during optimization. Program execution was not requested. # Notify the user that we have no output to generate. qmasm.abend( "Not writing the output file; optimization removed all weights and strengths" ) # Complain if we have disconnected qubits. discon_syms = logical_ising.find_disconnected_variables() if len(discon_syms) > 0: qmasm.abend("Disconnected variables encountered: %s" % " ".join(sorted(discon_syms)))
sys.stderr.write("# %s\n" % line) if bits_re.match(line): bits = [int(b) for b in list(line)] elif b"Energy of solution" in line: energy = float(line.split()[0]) retcode = proc.wait() if retcode < 0: os.kill(os.getpid(), -retcode) elif retcode > 0: # Some qbsolv errors go to stdout, not stderr. for line in proc.stdout: sys.stderr.write("qbsolv: %s\n" % line) sys.exit(retcode) sys.stderr.write("\n") # Fake various QMASM objects. qmasm.pin_weight = pin_weight qmasm.chain_strength = chain_strength for n, q in sorted(name2qubit.items(), key=lambda k: k[1]): qmasm.sym_map.new_symbol(n) answer = {"num_occurrences": [1], "energies": [energy], "solutions": [[2*b - 1 for b in bits]]} problem = qmasm.Problem(False) problem.embedding = [[i] for i in range(len(bits))] problem.embedder_chains = set() solutions = qmasm.Solutions(answer, problem, verbosity >= 2) # Output the solution. For now, we hard-wire show_asserts to False. qmasm.output_solution(solutions, style, verbosity, False)
id2solution = {} # Map from an int to a solution for snum in range(n_solns_to_output): soln = ValidSolution(physical_ising, final_answer[snum], energies[snum]) bad_assert = any([not a[1] for a in soln.check_assertions()]) if bad_assert: n_assertion_violations += 1 if not cl_args.all_solns: continue if soln.id not in id2solution: id2solution[soln.id] = soln # Output information about the raw solutions. if cl_args.verbose >= 1: sys.stderr.write("Number of solutions found:\n\n") sys.stderr.write(" %6d total\n" % len(energies)) sys.stderr.write(" %6d with no broken chains or broken pins\n" % num_not_broken) sys.stderr.write(" %6d at minimal energy\n" % n_low_energies) sys.stderr.write(" %6d with no failed assertions\n" % (n_low_energies - n_assertion_violations)) sys.stderr.write(" %6d excluding duplicate variable assignments\n" % len(id2solution)) sys.stderr.write("\n") # Output energy tallies. We first recompute these because some entries seem to # be multiply listed. if cl_args.verbose >= 2: qmasm.output_energy_tallies(physical_ising, answer, energies) # Output the solution to the standard output device. show_asserts = (cl_args.all_solns or cl_args.verbose >= 2) and len(physical_ising.assertions) > 0 qmasm.output_solution(id2solution, num_occurrences, cl_args.values, cl_args.verbose, show_asserts)
# The program is empty. Complain. qmasm.abend("Nothing to do (no weights or strengths specified)") elif cl_args.run: # The program was originally non-empty but was reduced to a empty # program during optimization. Program execution was requested. # Output the results we computed before exiting. qmasm.warn("Not executing the code; optimization produced a complete solution") num_value = {pre_simple_sym_map.to_number(sym): value for sym, value in logical_ising.known_values.items()} energy = pre_simple_problem.energy_from_known_values(num_value) physical_ising = logical_ising.add_fake_physical_fields() answer = {"solutions": [[]], "num_occurrences": [cl_args.samples], "energies": [energy]} solutions = qmasm.Solutions(answer, physical_ising, cl_args.verbose >= 2) show_asserts = cl_args.verbose >= 2 or cl_args.show in ["best", "all"] qmasm.output_solution(solutions, cl_args.values, cl_args.verbose, show_asserts) sys.exit(0) else: # The program was originally non-empty but was reduced to a empty # program during optimization. Program execution was not requested. # Notify the user that we have no output to generate. qmasm.abend("Not writing the output file; optimization removed all weights and strengths") # Complain if we have disconnected qubits. discon_syms = logical_ising.find_disconnected_variables() if len(discon_syms) > 0: qmasm.abend("Disconnected variables encountered: %s" % " ".join(sorted(discon_syms))) # Convert user-specified chains, anti-chains, and pins to assertions. logical_ising.append_assertions_from_statements()