def initializeQuantumProgram ( device, sim ): # *This function contains SDK specific code.* # # Input: # * *device* - String specifying the device on which the game is played. # Details about the device will be obtained using getLayout. # * *sim* - Whether this is a simulated run # Process: # * Initializes everything required by the SDK for the quantum program. The details depend on which SDK is used. # Output: # * *q* - Register of qubits (needed by both QISKit and ProjectQ). # * *c* - Register of classical bits (needed by QISKit only). # * *engine* - Class required to create programs in QISKit, ProjectQ and Forest. # * *script* - The quantum program, needed by QISKit and Forest. num, area, entangleType, pairs, pos, example, sdk, runs = getLayout(device) importSDK ( device ) if sdk in ["QISKit","ManualQISKit"]: engine = QuantumProgram() engine.set_api(Qconfig.APItoken, Qconfig.config["url"]) # set the APIToken and API url q = engine.create_quantum_register("q", num) c = engine.create_classical_register("c", num) script = engine.create_circuit("script", [q], [c]) elif sdk=="ProjectQ": engine = projectq.MainEngine() q = engine.allocate_qureg( num ) c = None script = None elif sdk=="Forest": if sim: engine = api.QVMConnection(use_queue=True) else: engine = api.QPUConnection(device) script = Program() q = range(num) c = range(num) return q, c, engine, script
import pyquil.api as forest qpu = forest.QPUConnection("Z12-13-C4a2") res = qpu.t1(qubit_id=3, start=0.01, stop=60.0, num_pts=31) print res.success, res.result
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)
import pyquil.quil as pq from pyquil.quil import Program from pyquil import api from pyquil.gates import * from pyquil.api import CompilerConnection, get_devices from pyquil.quil import Pragma from qutip import * import numpy as np devices = get_devices(as_dict=True) acorn = devices['19Q-Acorn'] compiler = CompilerConnection(acorn) qvm = api.QVMConnection() qpu = api.QPUConnection('19Q-Acorn') qubits = [[10, 16, 11, 17, 12], [4, 9, 14, 19, 13]] def postselect_had(data): newdata = [] for item in data: if (item[0] + item[2] + item[3]) % 2 == 0 and (item[1] + item[2]) % 2 == 0: newdata.append([item[-1]]) return newdata def Hadamard(ini, fin, qubit): p = Program(
def calculate_probs(): results = {} for basis in ["XX", "ZZ", "XZ", "ZX"]: if sim: engine = api.QVMConnection(use_queue=True) else: engine = api.QPUConnection(device) script = Program() # bell state script.inst(H(0)) script.inst(H(5)) script.inst(CZ(0, 5)) script.inst(RY(numpy.pi / 4, 0)) script.inst(H(0)) script.inst(H(5)) # set up bases if basis[0] == "X": script.inst(H(0)) if basis[1] == "X": script.inst(H(5)) # get and store results results[basis] = engine.run_and_measure(script, [0, 5], trials=trials) p = {} p["XI"] = 0 for basis in ["XX", "XZ"]: for sample in results[basis]: p["XI"] += sample[0] / (2 * trials) p["ZI"] = 0 for basis in ["ZX", "ZZ"]: for sample in results[basis]: p["ZI"] += sample[0] / (2 * trials) p["IX"] = 0 for basis in ["XX", "ZX"]: for sample in results[basis]: p["IX"] += sample[1] / (2 * trials) p["IZ"] = 0 for basis in ["XZ", "ZZ"]: for sample in results[basis]: p["IZ"] += sample[1] / (2 * trials) for basis in ["XX", "ZZ", "XZ", "ZX"]: p[basis] = 0 for sample in results[basis]: p[basis] += (sample[0] == sample[1]) / trials print(basis, p[basis]) #p["XX"] = 0 #p["XZ"] = 1 #p["ZX"] = 1 #p["ZZ"] = 1 return p