def postoptimize(prog): dev = Rget_devices(as_dict=True)['8Q-Agave'] comp = CompilerConnection(device=dev) pp1 = comp.compile(prog) for i in range(0): change = False ne_inst = [] for inst in pp1.instructions: if type(inst) is Gate: if inst.name == 'RZ': angle = inst.params[0] % 2 * pi print('RZ angle: ', angle, angle % 2 * pi) if min(angle, 2 * pi - angle) < 1e-3: change = True print('Removing.') continue ne_inst.append(inst) if not change: break #print('Recompiling...') pp1 = comp.compile(pq.Program(ne_inst)) return pp1
def compiler(): try: compiler = CompilerConnection(isa_source=ISA.from_dict(isa_dict())) compiler.compile(Program(I(0))) return compiler except (RequestException, UnknownApiError) as e: return pytest.skip( "This test requires compiler connection: {}".format(e))
def compile_INIT_gate(prog): compiler = CompilerConnection(acorn) compiledProg = compiler.compile(prog) qpuProg = Program() qpuProg.inst(compiledProg) return qpuProg
from pyquil.api import CompilerConnection, get_devices from pyquil.gates import X, H, CNOT, MEASURE from pyquil.quil import Program program = Program( # put your program here... X(0), ) agave = get_devices(as_dict=True)['8Q-Agave'] compiler = CompilerConnection(device=agave) compiled = compiler.compile(program) print(compiled)
def toy_piano_counterpoint(): pitch_index = int(request.args['pitch_index']) pitch_index %= (DIATONIC_SCALE_OCTAVE_PITCHES - 1) if pitch_index >= NUM_PITCHES: pitch_index = 0 species = int(request.args['species']) #print("species: ", species) melodic_degrees = request.args['melodic_degrees'].split(",") #print("melodic_degrees: ", melodic_degrees) harmonic_degrees = request.args['harmonic_degrees'].split(",") #print("harmonic_degrees: ", harmonic_degrees) use_simulator = request.args['use_simulator'].lower() == "true" print("use_simulator: ", use_simulator) compiler = None quantum_device = None q_con = None if use_simulator: q_con = api.QVMConnection() else: quantum_device = available_quantum_device() if quantum_device is not None: print('quantum_device: ', quantum_device) compiler = CompilerConnection(quantum_device) q_con = api.QPUConnection(quantum_device) # q_con = api.QVMConnection() else: # TODO: Test this condition print('No quantum devices available, using simulator') use_simulator = True q_con = api.QVMConnection() if (len(melodic_degrees) == DEGREES_OF_FREEDOM and len(harmonic_degrees) == DEGREES_OF_FREEDOM and 1 <= species <= 3 and 0 <= pitch_index < NUM_PITCHES): #TODO: Move/change this rot_melodic_circuit = compute_circuit(melodic_degrees) if not use_simulator: # TODO: Put back in # rot_melodic_circuit = compiler.compile(rot_melodic_circuit) # TODO: Remove these lines tmp_rot_melodic_circuit = compiler.compile(rot_melodic_circuit) # print("tmp_rot_melodic_circuit:") # print(tmp_rot_melodic_circuit) rot_melodic_circuit = Program() for instruction in tmp_rot_melodic_circuit.instructions: if not isinstance(instruction, Pragma): rot_melodic_circuit.inst(instruction) print("rot_melodic_circuit:") print(rot_melodic_circuit) rot_harmonic_circuit = compute_circuit(harmonic_degrees) if not use_simulator: # TODO: Put back in # rot_harmonic_circuit = compiler.compile(rot_harmonic_circuit) # TODO: Remove these lines tmp_rot_harmonic_circuit = compiler.compile(rot_harmonic_circuit) # print("tmp_rot_harmonic_circuit:") # print(tmp_rot_harmonic_circuit) rot_harmonic_circuit = Program() for instruction in tmp_rot_harmonic_circuit.instructions: if not isinstance(instruction, Pragma): rot_harmonic_circuit.inst(instruction) print("rot_harmonic_circuit:") print(rot_harmonic_circuit) harmony_notes_factor = 2**( species - 1) # Number of harmony notes for each melody note num_composition_bits = TOTAL_MELODY_NOTES * (harmony_notes_factor + 1) * NUM_CIRCUIT_WIRES composition_bits = [0] * num_composition_bits # Convert the pitch index to a binary string, and place into the # composition_bits array, least significant bits in lowest elements of array qubit_string = format(pitch_index, '03b') for idx, qubit_char in enumerate(qubit_string): if qubit_char == '0': composition_bits[idx] = 0 else: composition_bits[idx] = 1 num_runs = 1 # Compute notes for the main melody for melody_note_idx in range(0, TOTAL_MELODY_NOTES): # if (melody_note_idx < TOTAL_MELODY_NOTES - 1): p = Program() for bit_idx in range(0, NUM_CIRCUIT_WIRES): if (composition_bits[melody_note_idx * NUM_CIRCUIT_WIRES + bit_idx] == 0): p.inst(I(NUM_CIRCUIT_WIRES - 1 - bit_idx)) else: p.inst(X(NUM_CIRCUIT_WIRES - 1 - bit_idx)) p.inst(copy.deepcopy(rot_melodic_circuit)) p.inst().measure(0, 0).measure(1, 1) \ .measure(2, 2) # print("rot_melodic_circuit:") # print(p) result = q_con.run(p, [2, 1, 0], num_runs) bits = result[0] for bit_idx in range(0, NUM_CIRCUIT_WIRES): composition_bits[(melody_note_idx + 1) * NUM_CIRCUIT_WIRES + bit_idx] = bits[bit_idx] #print(composition_bits) measured_pitch = bits[0] * 4 + bits[1] * 2 + bits[2] #print("melody melody_note_idx measured_pitch") #print(melody_note_idx) #print(measured_pitch) # Now compute a harmony note for the melody note #print("Now compute a harmony note for the melody notev") p = Program() for bit_idx in range(0, NUM_CIRCUIT_WIRES): if composition_bits[melody_note_idx * NUM_CIRCUIT_WIRES + bit_idx] == 0: p.inst(I(NUM_CIRCUIT_WIRES - 1 - bit_idx)) else: p.inst(X(NUM_CIRCUIT_WIRES - 1 - bit_idx)) p.inst(copy.deepcopy(rot_harmonic_circuit)) p.inst().measure(0, 0).measure(1, 1) \ .measure(2, 2) # print("rot_harmonic_circuit:") # print(p) result = q_con.run(p, [2, 1, 0], num_runs) bits = result[0] for bit_idx in range(0, NUM_CIRCUIT_WIRES): composition_bits[(melody_note_idx * NUM_CIRCUIT_WIRES * harmony_notes_factor) + (TOTAL_MELODY_NOTES * NUM_CIRCUIT_WIRES) + bit_idx] = bits[bit_idx] #print(composition_bits) measured_pitch = bits[0] * 4 + bits[1] * 2 + bits[2] #print("harmony melody_note_idx measured_pitch") #print(melody_note_idx) #print(measured_pitch) # Now compute melody notes to follow the harmony note #print("Now compute melody notes to follow the harmony note") for harmony_note_idx in range(1, harmony_notes_factor): p = Program() for bit_idx in range(0, NUM_CIRCUIT_WIRES): if (composition_bits[ (melody_note_idx * NUM_CIRCUIT_WIRES * harmony_notes_factor) + ((harmony_note_idx - 1) * NUM_CIRCUIT_WIRES) + (TOTAL_MELODY_NOTES * NUM_CIRCUIT_WIRES) + bit_idx] == 0): p.inst(I(NUM_CIRCUIT_WIRES - 1 - bit_idx)) else: p.inst(X(NUM_CIRCUIT_WIRES - 1 - bit_idx)) p.inst(copy.deepcopy(rot_melodic_circuit)) p.inst().measure(0, 0).measure(1, 1) \ .measure(2, 2) #print("rot_melodic_circuit:") #print(p) result = q_con.run(p, [2, 1, 0], num_runs) bits = result[0] for bit_idx in range(0, NUM_CIRCUIT_WIRES): composition_bits[(melody_note_idx * NUM_CIRCUIT_WIRES * harmony_notes_factor) + ((harmony_note_idx) * NUM_CIRCUIT_WIRES) + (TOTAL_MELODY_NOTES * NUM_CIRCUIT_WIRES) + bit_idx] = bits[bit_idx] #print(composition_bits) measured_pitch = bits[0] * 4 + bits[1] * 2 + bits[2] #print("melody after harmony melody_note_idx measured_pitch") #print(melody_note_idx) #print(measured_pitch) all_note_nums = create_note_nums_array(composition_bits) melody_note_nums = all_note_nums[0:TOTAL_MELODY_NOTES] harmony_note_nums = all_note_nums[7:num_composition_bits] if use_simulator: composer = "Rigetti QVM" else: composer = "Rigetti " + "8Q-Agave" ret_dict = { "melody": melody_note_nums, "harmony": harmony_note_nums, "lilypond": create_lilypond(melody_note_nums, harmony_note_nums, composer), "toy_piano": create_toy_piano(melody_note_nums, harmony_note_nums) } return jsonify(ret_dict)