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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
```json
|
| 2 |
+
{
|
| 3 |
+
"cells": [
|
| 4 |
+
{
|
| 5 |
+
"cell_type": "markdown",
|
| 6 |
+
"metadata": {},
|
| 7 |
+
"source": [
|
| 8 |
+
"# Quantum Music Synthesizer\n",
|
| 9 |
+
"\n",
|
| 10 |
+
"This notebook demonstrates how to create music using quantum computing principles with the QuantumToolbox.jl library."
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": null,
|
| 16 |
+
"metadata": {},
|
| 17 |
+
"outputs": [],
|
| 18 |
+
"source": [
|
| 19 |
+
"# Install required packages\n",
|
| 20 |
+
"using Pkg\n",
|
| 21 |
+
"Pkg.add(\"QuantumToolbox\")\n",
|
| 22 |
+
"Pkg.add(\"Plots\")\n",
|
| 23 |
+
"Pkg.add(\"AudioTracks\")"
|
| 24 |
+
]
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"cell_type": "code",
|
| 28 |
+
"execution_count": null,
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"outputs": [],
|
| 31 |
+
"source": [
|
| 32 |
+
"# Import libraries\n",
|
| 33 |
+
"using QuantumToolbox\n",
|
| 34 |
+
"using Plots\n",
|
| 35 |
+
"using AudioTracks\n",
|
| 36 |
+
"using LinearAlgebra"
|
| 37 |
+
]
|
| 38 |
+
},
|
| 39 |
+
{
|
| 40 |
+
"cell_type": "code",
|
| 41 |
+
"execution_count": null,
|
| 42 |
+
"metadata": {},
|
| 43 |
+
"outputs": [],
|
| 44 |
+
"source": [
|
| 45 |
+
"# Define quantum music parameters\n",
|
| 46 |
+
"n_qubits = 4\n",
|
| 47 |
+
"n_shots = 1024\n",
|
| 48 |
+
"fidelity_threshold = 0.995\n",
|
| 49 |
+
"\n",
|
| 50 |
+
"# Create a quantum circuit for music generation\n",
|
| 51 |
+
"function create_music_circuit(n_qubits)\n",
|
| 52 |
+
" qc = qubit_states(n_qubits)\n",
|
| 53 |
+
" \n",
|
| 54 |
+
" # Apply superposition to all qubits\n",
|
| 55 |
+
" for i in 1:n_qubits\n",
|
| 56 |
+
" qc = hadamard(qc, i)\n",
|
| 57 |
+
" end\n",
|
| 58 |
+
" \n",
|
| 59 |
+
" # Create entanglement between qubits\n",
|
| 60 |
+
" for i in 1:(n_qubits-1)\n",
|
| 61 |
+
" qc = cnot(qc, i, i+1)\n",
|
| 62 |
+
" end\n",
|
| 63 |
+
" \n",
|
| 64 |
+
" return qc\n",
|
| 65 |
+
"end\n",
|
| 66 |
+
"\n",
|
| 67 |
+
"# Generate quantum circuit\n",
|
| 68 |
+
"circuit = create_music_circuit(n_qubits)\n",
|
| 69 |
+
"println(\"Quantum circuit created with $n_qubits qubits\")"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "code",
|
| 74 |
+
"execution_count": null,
|
| 75 |
+
"metadata": {},
|
| 76 |
+
"outputs": [],
|
| 77 |
+
"source": [
|
| 78 |
+
"# Simulate quantum measurements\n",
|
| 79 |
+
"function simulate_quantum_music(circuit, n_shots)\n",
|
| 80 |
+
" # Measure the circuit multiple times\n",
|
| 81 |
+
" measurements = []\n",
|
| 82 |
+
" for i in 1:n_shots\n",
|
| 83 |
+
" result = measure(circuit)\n",
|
| 84 |
+
" push!(measurements, result)\n",
|
| 85 |
+
" end\n",
|
| 86 |
+
" return measurements\n",
|
| 87 |
+
"end\n",
|
| 88 |
+
"\n",
|
| 89 |
+
"# Run simulation\n",
|
| 90 |
+
"measurements = simulate_quantum_music(circuit, n_shots)\n",
|
| 91 |
+
"println(\"Simulated $(length(measurements)) quantum measurements\")"
|
| 92 |
+
]
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"cell_type": "code",
|
| 96 |
+
"execution_count": null,
|
| 97 |
+
"metadata": {},
|
| 98 |
+
"outputs": [],
|
| 99 |
+
"source": [
|
| 100 |
+
"# Convert quantum measurements to musical notes\n",
|
| 101 |
+
"function quantum_to_notes(measurements)\n",
|
| 102 |
+
" # Base frequencies for C major scale (in Hz)\n",
|
| 103 |
+
" base_frequencies = [261.63, 293.66, 329.63, 349.23, 392.00, 440.00, 493.88, 523.25] # C4 to C5\n",
|
| 104 |
+
" \n",
|
| 105 |
+
" notes = []\n",
|
| 106 |
+
" for measurement in measurements\n",
|
| 107 |
+
" # Convert binary measurement to decimal index\n",
|
| 108 |
+
" index = parse(Int, join(string.(measurement)), base=2) % length(base_frequencies) + 1\n",
|
| 109 |
+
" push!(notes, base_frequencies[index])\n",
|
| 110 |
+
" end\n",
|
| 111 |
+
" \n",
|
| 112 |
+
" return notes\n",
|
| 113 |
+
"end\n",
|
| 114 |
+
"\n",
|
| 115 |
+
"# Convert measurements to notes\n",
|
| 116 |
+
"notes = quantum_to_notes(measurements)\n",
|
| 117 |
+
"println(\"Converted to $(length(notes)) musical notes\")\n",
|
| 118 |
+
"println(\"First 10 notes: $(notes[1:10])\")"
|
| 119 |
+
]
|
| 120 |
+
},
|
| 121 |
+
{
|
| 122 |
+
"cell_type": "code",
|
| 123 |
+
"execution_count": null,
|
| 124 |
+
"metadata": {},
|
| 125 |
+
"outputs": [],
|
| 126 |
+
"source": [
|
| 127 |
+
"# Create audio from notes\n",
|
| 128 |
+
"function create_audio_from_notes(notes, duration=0.5)\n",
|
| 129 |
+
" # Create a simple sine wave for each note\n",
|
| 130 |
+
" sample_rate = 44100\n",
|
| 131 |
+
" audio = []\n",
|
| 132 |
+
" \n",
|
| 133 |
+
" for note in notes[1:50] # Limit to first 50 notes for demonstration\n",
|
| 134 |
+
" t = 0:1/sample_rate:duration\n",
|
| 135 |
+
" wave = sin.(2π * note * t)\n",
|
| 136 |
+
" append!(audio, wave)\n",
|
| 137 |
+
" end\n",
|
| 138 |
+
" \n",
|
| 139 |
+
" return audio, sample_rate\n",
|
| 140 |
+
"end\n",
|
| 141 |
+
"\n",
|
| 142 |
+
"# Generate audio\n",
|
| 143 |
+
"audio_data, sample_rate = create_audio_from_notes(notes)\n",
|
| 144 |
+
"println(\"Generated audio with $(length(audio_data)) samples at $(sample_rate)Hz sample rate\")"
|
| 145 |
+
]
|
| 146 |
+
},
|
| 147 |
+
{
|
| 148 |
+
"cell_type": "code",
|
| 149 |
+
"execution_count": null,
|
| 150 |
+
"metadata": {},
|
| 151 |
+
"outputs": [],
|
| 152 |
+
"source": [
|
| 153 |
+
"# Visualize quantum state probabilities\n",
|
| 154 |
+
"function plot_quantum_probabilities(circuit, n_qubits)\n",
|
| 155 |
+
" # Calculate probabilities for all possible states\n",
|
| 156 |
+
" states = [bitstring(i, n_qubits) for i in 0:(2^n_qubits-1)]\n",
|
| 157 |
+
" probabilities = [abs(amplitude(circuit, parse.(Int, split(state, \"\"))))^2 for state in states]\n",
|
| 158 |
+
" \n",
|
| 159 |
+
" # Create bar chart\n",
|
| 160 |
+
" bar(\n",
|
| 161 |
+
" 0:(2^n_qubits-1),\n",
|
| 162 |
+
" probabilities,\n",
|
| 163 |
+
" xlabel=\"Quantum States\",\n",
|
| 164 |
+
" ylabel=\"Probability\",\n",
|
| 165 |
+
" title=\"Quantum State Probabilities\",\n",
|
| 166 |
+
" legend=false,\n",
|
| 167 |
+
" color=:purple\n",
|
| 168 |
+
" )\n",
|
| 169 |
+
"end\n",
|
| 170 |
+
"\n",
|
| 171 |
+
"# Plot probabilities\n",
|
| 172 |
+
"plot_quantum_probabilities(circuit, n_qubits)"
|
| 173 |
+
]
|
| 174 |
+
},
|
| 175 |
+
{
|
| 176 |
+
"cell_type": "code",
|
| 177 |
+
"execution_count": null,
|
| 178 |
+
"metadata": {},
|
| 179 |
+
"outputs": [],
|
| 180 |
+
"source": [
|
| 181 |
+
"# Analyze quantum circuit fidelity\n",
|
| 182 |
+
"function analyze_fidelity(measurements)\n",
|
| 183 |
+
" # Calculate the distribution of measurements\n",
|
| 184 |
+
" counts = Dict{String, Int}()\n",
|
| 185 |
+
" for measurement in measurements\n",
|
| 186 |
+
" key = join(string.(measurement))\n",
|
| 187 |
+
" counts[key] = get(counts, key, 0) + 1\n",
|
| 188 |
+
" end\n",
|
| 189 |
+
" \n",
|
| 190 |
+
" # Calculate expected uniform distribution\n",
|
| 191 |
+
" n_states = length(unique([join(string.(m)) for m in measurements]))\n",
|
| 192 |
+
" expected_prob = 1.0 / n_states\n",
|
| 193 |
+
" \n",
|
| 194 |
+
" # Calculate fidelity\n",
|
| 195 |
+
" fidelity = 0.0\n",
|
| 196 |
+
" for (state, count) in counts\n",
|
| 197 |
+
" observed_prob = count / length(measurements)\n",
|
| 198 |
+
" fidelity += sqrt(observed_prob * expected_prob)\n",
|
| 199 |
+
" end\n",
|
| 200 |
+
" fidelity = fidelity^2\n",
|
| 201 |
+
" \n",
|
| 202 |
+
" return fidelity, counts\n",
|
| 203 |
+
"end\n",
|
| 204 |
+
"\n",
|
| 205 |
+
"# Analyze results\n",
|
| 206 |
+
"fidelity, state_counts = analyze_fidelity(measurements)\n",
|
| 207 |
+
"println(\"Quantum circuit fidelity: $(round(fidelity, digits=4))\")\n",
|
| 208 |
+
"println(\"Fidelity meets threshold: $(fidelity >= fidelity_threshold)\")\n",
|
| 209 |
+
"println(\"\\nState distribution:\")\n",
|
| 210 |
+
"for (state, count) in sort(collect(state_counts))\n",
|
| 211 |
+
" println(\" $state: $count\")\n",
|
| 212 |
+
"end"
|
| 213 |
+
]
|
| 214 |
+
},
|
| 215 |
+
{
|
| 216 |
+
"cell_type": "markdown",
|
| 217 |
+
"metadata": {},
|
| 218 |
+
"source": [
|
| 219 |
+
"## Summary\n",
|
| 220 |
+
"\n",
|
| 221 |
+
"This notebook demonstrated:\n",
|
| 222 |
+
"1. Creating a quantum circuit for music generation\n",
|
| 223 |
+
"2. Simulating quantum measurements\n",
|
| 224 |
+
"3. Converting quantum states to musical notes\n",
|
| 225 |
+
"4. Generating audio from quantum measurements\n",
|
| 226 |
+
"5. Visualizing quantum state probabilities\n",
|
| 227 |
+
"6. Analyzing quantum circuit fidelity\n",
|
| 228 |
+
"\n",
|
| 229 |
+
"The quantum music synthesizer leverages quantum superposition and entanglement to create unique musical compositions that would be impossible with classical methods alone."
|
| 230 |
+
]
|
| 231 |
+
}
|
| 232 |
+
],
|
| 233 |
+
"metadata": {
|
| 234 |
+
"kernelspec": {
|
| 235 |
+
"display_name": "Julia 1.8.0",
|
| 236 |
+
"language": "julia",
|
| 237 |
+
"name": "julia-1.8"
|
| 238 |
+
},
|
| 239 |
+
"language_info": {
|
| 240 |
+
"file_extension": ".jl",
|
| 241 |
+
"mimetype": "application/julia",
|
| 242 |
+
"name": "julia",
|
| 243 |
+
"version": "1.8.0"
|
| 244 |
+
}
|
| 245 |
+
},
|
| 246 |
+
"nbformat": 4,
|
| 247 |
+
"nbformat_minor": 4
|
| 248 |
+
}
|
| 249 |
+
```
|