-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathquantum_toy_piano_service_ibmq.py
452 lines (360 loc) · 17.9 KB
/
quantum_toy_piano_service_ibmq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#
# Copyright 2018 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister
from qiskit import execute, IBMQ, BasicAer
from qiskit.providers.ibmq import least_busy
from math import *
import numpy as np
from flask import Flask, jsonify, request
from flask_cors import CORS
import copy
from s04_rotcircuit_ibmq import *
from collections import deque
import time
# from qiskit import register
try:
provider = IBMQ.load_account()
except:
print("""WARNING: Could not load your IBMQ backend account.
Configure your IBMQ credentials using 'IMBQ.save_account'
The method is described here
https://github.com/Qiskit/qiskit-ibmq-provider """)
app = Flask(__name__)
CORS(app)
DEGREES_OF_FREEDOM = 6
# NUM_PITCHES = 4
DIATONIC_SCALE_OCTAVE_PITCHES = 8
NUM_CIRCUIT_WIRES = 3
TOTAL_MELODY_NOTES = 7
CIRCUIT_RESULT_KEY_LENGTH = 5 # '000_m' is such a key, for example
###
# Produces a musical (specifically second-species counterpoint) composition for
# a given initial pitch, and melodic/harmonic rotations degrees. This operates in a degraded mode,
# in that a call to the quantum computer or simulator is made for each note in the resulting
# composition.
# Parameters:
# pitch_index Index (0 - 3) of the initial pitch for which a composition is desired. This determines
# the mode (Ionian, Dorian, etc.) of the composition
# species Number (1 - 3) representing the species of counterpoint desired
# melodic_degrees Comma-delimited string containing 6 rotations in degrees for melody matrix
# harmonic_degrees Comma-delimited string containing 6 rotations in degrees for harmony matrix
#
# Returns JSON string containing:
# melody_part
# pitch_index
# start_beat
# pitch_probs
# harmony_part
# pitch_index
# start_beat
# pitch_probs
#
# pitch_index is an integer from (0 - 7) resulting from measurement
# start_beat is the beat in the entire piece for which the note was produced
# pitch_probs is an array of eight probabilities from which the pitch_index resulted
###
@app.route('/toy_piano_counterpoint')
def toy_piano_counterpoint():
pitch_index = int(request.args['pitch_index'])
if pitch_index >= DIATONIC_SCALE_OCTAVE_PITCHES:
# pitch_index = 0
pitch_index %= (DIATONIC_SCALE_OCTAVE_PITCHES - 1)
species = int(request.args['species'])
melodic_degrees = request.args['melodic_degrees'].split(",")
print("melodic_degrees: ", melodic_degrees)
harmonyenabled = True
harmonic_degrees = []
harmonic_degrees_str = request.args['harmonic_degrees']
if len(harmonic_degrees_str) > 0:
harmonic_degrees = harmonic_degrees_str.split(",")
else:
harmonyenabled = False
print("harmonic_degrees: ", harmonic_degrees)
use_simulator = request.args['use_simulator'].lower() == "true"
print()
print("use_simulator: ", use_simulator)
onlyReturnJamNotes = False
# If 0 species passed in, compute a species 3 to make maximum number of jam notes
if species == 0:
onlyReturnJamNotes = True
species = 3
if (len(melodic_degrees) == DEGREES_OF_FREEDOM and
(len(harmonic_degrees) == DEGREES_OF_FREEDOM or not harmonyenabled) and
1 <= species <= 3 and
0 <= pitch_index < DIATONIC_SCALE_OCTAVE_PITCHES):
circuit_dict = {} # Key is circuit name, value is circuit
res_dict = dict()
res_dict['000_m'] = deque([])
res_dict['000_h'] = deque([])
res_dict['001_m'] = deque([])
res_dict['001_h'] = deque([])
res_dict['010_m'] = deque([])
res_dict['010_h'] = deque([])
res_dict['011_m'] = deque([])
res_dict['011_h'] = deque([])
res_dict['100_m'] = deque([])
res_dict['100_h'] = deque([])
res_dict['101_m'] = deque([])
res_dict['101_h'] = deque([])
res_dict['110_m'] = deque([])
res_dict['110_h'] = deque([])
res_dict['111_m'] = deque([])
res_dict['111_h'] = deque([])
q_reg = QuantumRegister(3)
c_req = ClassicalRegister(3)
qc_melodic = QuantumCircuit(q_reg, c_req)
rot_melodic_circuit = compute_circuit(melodic_degrees, q_reg, c_req, qc_melodic)
print("rot_melodic_circuit.qasm(): ", rot_melodic_circuit.qasm())
if harmonyenabled:
qc_harmonic = QuantumCircuit(q_reg, c_req)
rot_harmonic_circuit = compute_circuit(harmonic_degrees, q_reg, c_req, qc_harmonic)
# Create all of the potentially required melody circuits
# TODO: Generalize to handle any number of pitches, and species, and remove hardcoded values
# Note: 11 melody, 7 harmony is currently a small enough batch for IBMQ devices
num_required_melodic_circuits_per_pitch = (11 if use_simulator else 6) # 6 for first, 16 for second, 27 for third-species
if species == 2:
num_required_melodic_circuits_per_pitch = (11 if use_simulator else 6)
elif species == 1:
num_required_melodic_circuits_per_pitch = (11 if use_simulator else 6)
num_required_harmonic_circuits_per_pitch = ((8 if use_simulator else 3) if harmonyenabled else 0)
# input_pitch = 0
for pitch_idx in range(0, DIATONIC_SCALE_OCTAVE_PITCHES):
for melodic_circuit_idx in range(0, num_required_melodic_circuits_per_pitch):
input_qc = QuantumCircuit(q_reg, c_req)
qubit_string = format(pitch_idx, '03b') # TODO: Use NUM_CIRCUIT_WIRES in format string
# print (qubit_string + ":" + str(melodic_circuit_idx))
for char_idx in range(NUM_CIRCUIT_WIRES):
if qubit_string[char_idx] == '0':
input_qc.iden(q_reg[NUM_CIRCUIT_WIRES - 1 - char_idx])
else:
input_qc.x(q_reg[NUM_CIRCUIT_WIRES - 1 - char_idx])
input_qc.extend(rot_melodic_circuit)
circuit_dict[qubit_string + "_m_" + format(melodic_circuit_idx, '02')] = input_qc
if harmonyenabled:
input_pitch = 0
for pitch_idx in range(0, DIATONIC_SCALE_OCTAVE_PITCHES):
for harmonic_circuit_idx in range(0, num_required_harmonic_circuits_per_pitch):
input_qc = QuantumCircuit(q_reg, c_req)
qubit_string = format(pitch_idx, '03b') # TODO: Use NUM_CIRCUIT_WIRES in format string
# print (qubit_string + ":" + str(harmonic_circuit_idx))
for char_idx in range(NUM_CIRCUIT_WIRES):
if qubit_string[char_idx] == '0':
input_qc.iden(q_reg[NUM_CIRCUIT_WIRES - 1 - char_idx])
else:
input_qc.x(q_reg[NUM_CIRCUIT_WIRES - 1 - char_idx])
input_qc.extend(rot_harmonic_circuit)
circuit_dict[qubit_string + "_h_" + format(harmonic_circuit_idx, '02')] = input_qc
# print('input_qc.qasm(): ', input_qc.qasm())
#print(circuit_dict)
quantum_backend = BasicAer.get_backend('qasm_simulator')
if use_simulator:
pass
else:
ibmq_backends = provider.backends()
print("Remote backends: ", ibmq_backends)
try:
quantum_backend = least_busy(provider.backends(simulator=False))
except:
print("All devices are currently unavailable.")
print('quantum_backend: ', quantum_backend)
composer = str(quantum_backend)
print('composer: ', composer)
job_exp = execute(list(circuit_dict.values()), quantum_backend, shots=1)
# try:
job_id = job_exp.job_id() # It will block until completing submission.
print('The job {} was successfully submitted'.format(job_id))
job_result = job_exp.result() # It will block until finishing.
print('The job finished with result {}'.format(job_result))
# except JobError as ex:
# print("Something wrong happened!: {}".format(ex))
print(job_exp.status)
for circuit_name in circuit_dict.keys():
# print(circuit_name)
bitstr = list(job_result.get_counts(circuit_dict[circuit_name]).keys())[0]
# bitstr = list(sim_result.get_counts(circuit_name).keys())[0]
res_dict[circuit_name[0:CIRCUIT_RESULT_KEY_LENGTH]].append(bitstr)
# print(bitstr)
full_res_dict = dict()
for key in res_dict:
full_res_dict[key] = list(res_dict[key])
print(res_dict)
harmony_notes_factor = 2**(species - 1) * (1 if harmonyenabled else 0) # 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):
res_dict_key = ""
for bit_idx in range(0, NUM_CIRCUIT_WIRES):
res_dict_key += str(composition_bits[melody_note_idx * NUM_CIRCUIT_WIRES + bit_idx])
res_dict_key += "_m"
# Insert a treble-clef C if no more measurements for given note to be popped
if res_dict[res_dict_key]:
bitstr = res_dict[res_dict_key].popleft()
else:
print("Queue " + res_dict_key + " is empty" )
bitstr = "111"
# print("mel res_dict_key bitstr:")
# print(res_dict_key + "_" + bitstr)
for bit_idx in range(0, NUM_CIRCUIT_WIRES):
composition_bits[(melody_note_idx + 1) * NUM_CIRCUIT_WIRES + bit_idx] = int(bitstr[bit_idx])
# print(res_dict)
if harmonyenabled:
# Now compute a harmony note for the melody note
res_dict_key = ""
for bit_idx in range(0, NUM_CIRCUIT_WIRES):
res_dict_key += str(composition_bits[melody_note_idx * NUM_CIRCUIT_WIRES + bit_idx])
res_dict_key += "_h"
bitstr = res_dict[res_dict_key].popleft()
# print("har res_dict_key bitstr:")
# print(res_dict_key + "_" + bitstr)
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] = int(bitstr[bit_idx])
# print(res_dict)
# Now compute melody notes to follow the harmony note
for harmony_note_idx in range(1, harmony_notes_factor):
res_dict_key = ""
for bit_idx in range(0, NUM_CIRCUIT_WIRES):
res_dict_key += str(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])
res_dict_key += "_m"
# Insert a treble-clef C if no more measurements for given note to be popped
if res_dict[res_dict_key]:
bitstr = res_dict[res_dict_key].popleft()
else:
print("Queue " + res_dict_key + " is empty" )
bitstr = "111"
# print("melb res_dict_key bitstr:")
# print(res_dict_key + "_" + bitstr)
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] = int(bitstr[bit_idx])
print()
print(res_dict)
all_note_nums = create_note_nums_array(composition_bits)
melody_note_nums = all_note_nums[0:TOTAL_MELODY_NOTES]
harmony_note_nums = []
if harmonyenabled:
harmony_note_nums = all_note_nums[7:num_composition_bits]
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),
"full_res_dict": full_res_dict}
if onlyReturnJamNotes:
ret_dict["lilypond"] = ''
ret_dict["toy_piano"] = []
return jsonify(ret_dict)
def create_note_nums_array(ordered_classical_registers):
allnotes_array = []
cur_val = 0
for idx, bit in enumerate(ordered_classical_registers):
if idx % 3 == 0:
cur_val += bit * 4
elif idx % 3 == 1:
cur_val += bit * 2
else:
cur_val += bit
allnotes_array.append(cur_val)
cur_val = 0
return allnotes_array
def pitch_letter_by_index(pitch_idx):
retval = "z"
if pitch_idx == 0:
retval = "c"
elif pitch_idx == 1:
retval = "d"
elif pitch_idx == 2:
retval = "e"
elif pitch_idx == 3:
retval = "f"
elif pitch_idx == 4:
retval = "g"
elif pitch_idx == 5:
retval = "a"
elif pitch_idx == 6:
retval = "b"
elif pitch_idx == 7:
retval = "c'"
else:
retval = "z"
return retval
# Produce output for Lilypond
def create_lilypond(melody_note_nums, harmony_note_nums, composer):
harmony_notes_fact = int(len(harmony_note_nums) / len(melody_note_nums))
harmonyenabled = harmony_notes_fact > 0
retval = "\\version \"2.18.2\" \\paper {#(set-paper-size \"a5\")} " +\
" \\header {title=\"Schrodinger's Cat\" subtitle=\"on a Toy Piano\" composer = \"" + composer + "\"} " + \
" melody = \\absolute { \\clef " + \
(" \"bass\" " if harmonyenabled else " \"treble\" ") + \
" \\numericTimeSignature \\time 4/4 \\tempo 4 = 100"
for pitch in melody_note_nums:
retval += " " + pitch_letter_by_index(pitch) + ("" if harmonyenabled else "'") + ("2" if harmonyenabled else "4")
# Add the same pitch to the end of the melody as in the beginning
retval += " " + pitch_letter_by_index(melody_note_nums[0]) + ("" if harmonyenabled else "'") + ("2" if harmonyenabled else "4")
if harmonyenabled:
retval += "} harmony = \\absolute { \\clef \"treble\" \\numericTimeSignature \\time 4/4 "
for pitch in harmony_note_nums:
retval += " " + pitch_letter_by_index(pitch) + "'" + str(int(harmony_notes_fact * 2))
# Add the same pitch to the end of the harmony as in the beginning of the melody,
# only an octave higher
retval += " " + pitch_letter_by_index(melody_note_nums[0]) + "'2"
retval += "} \\score { << "
if harmonyenabled:
retval += " \\new Staff \\with {instrumentName = #\"Harmony\"} { \\harmony } "
retval += " \\new Staff \\with {instrumentName = #\"Melody\"} { \\melody } >> }"
return retval
# Produce output for toy piano
def create_toy_piano(melody_note_nums, harmony_note_nums):
harmony_notes_fact = int(len(harmony_note_nums) / len(melody_note_nums))
harmonyenabled = harmony_notes_fact > 0
quarter_note_dur = 150
notes = []
latest_melody_idx = 0
latest_harmony_idx = 0
num_pitches_in_octave = 7
toy_piano_pitch_offset = 8
for idx, pitch in enumerate(melody_note_nums):
notes.append({"num": pitch + toy_piano_pitch_offset + (0 if harmonyenabled else num_pitches_in_octave), "time": idx * quarter_note_dur * (2 if harmonyenabled else 1)})
latest_melody_idx = idx
# Add the same pitch to the end of the melody as in the beginning
notes.append({"num": melody_note_nums[0] + toy_piano_pitch_offset + (0 if harmonyenabled else num_pitches_in_octave), "time": (latest_melody_idx + 1) * quarter_note_dur * (2 if harmonyenabled else 1)})
if harmonyenabled:
for idx, pitch in enumerate(harmony_note_nums):
notes.append({"num": pitch + num_pitches_in_octave + toy_piano_pitch_offset, "time": idx * quarter_note_dur * 2 / harmony_notes_fact})
latest_harmony_idx = idx
# Add the same pitch to the end of the harmony as in the beginning of the melody,
# only an octave higher
notes.append({"num": melody_note_nums[0] + num_pitches_in_octave + toy_piano_pitch_offset, "time": (latest_harmony_idx + 1) * quarter_note_dur * 2 / harmony_notes_fact})
# Sort the array of dictionaries by time
sorted_notes = sorted(notes, key=lambda k: k['time'])
return sorted_notes
if __name__ == '__main__':
# app.run()
app.run(host='127.0.0.1', port=5002)