Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coconutshell (Team 10) #3

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added IonQ - 10.pdf
Binary file not shown.
232 changes: 232 additions & 0 deletions quantum_phen.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [],
"source": [
"import pennylane as qml\n",
"\n",
"\n",
"\n",
"\n",
"dev = qml.device('default.qubit', wires=5, shots=1)"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [],
"source": [
"\n",
"\n",
"@qml.qnode(dev)\n",
"def quantum_walk(input_node):\n",
"\n",
" for i in range(3):\n",
"\n",
" if input_node[i]==1:\n",
" qml.PauliX(i)\n",
"\n",
" # Add a Hadamard gate to qubit 0\n",
" qml.Hadamard(3)\n",
" qml.Hadamard(4)\n",
" # Perform a controlled-X gate on qubit 1, controlled by qubit 0\n",
" qml.CNOT([4, 0])\n",
" qml.PauliX(4)\n",
" qml.CNOT([4, 1])\n",
" qml.CNOT([3, 2])\n",
" qml.MultiControlledX([4,3], 1)\n",
" qml.PauliX(4)\n",
" qml.MultiControlledX([4,3], 0)\n",
" qml.PauliX(4)\n",
" qml.MultiControlledX([4,3], 2)\n",
"\n",
" return qml.sample()"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0: ──X─╭X────────────────╭X───────┤ Sample\n",
"1: ────│─────╭X────╭X────│────────┤ Sample\n",
"2: ────│─────│──╭X─│─────│─────╭X─┤ Sample\n",
"3: ──H─│─────│──╰●─├●────├●────├●─┤ Sample\n",
"4: ──H─╰●──X─╰●────╰●──X─╰●──X─╰●─┤ Sample\n"
]
}
],
"source": [
"print(qml.draw(quantum_walk)([1,0,0]))"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"def step(v):\n",
" \n",
" return quantum_walk(v)[:3]\n"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"pitch_mapping = {\n",
" '000': 48, # C3\n",
" '001': 52, # E3\n",
" '010': 55, # G3\n",
" '011': 59, # B3\n",
" '100': 60, # C4 (Middle C)\n",
" '101': 64, # E4\n",
" '110': 67, # G4\n",
" '111': 71, # B4\n",
"}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"48\n",
"Pitch: 48, Type: <class 'int'>\n",
"60\n",
"Pitch: 60, Type: <class 'int'>\n",
"67\n",
"Pitch: 67, Type: <class 'int'>\n",
"55\n",
"Pitch: 55, Type: <class 'int'>\n",
"55\n",
"Pitch: 55, Type: <class 'int'>\n",
"48\n",
"Pitch: 48, Type: <class 'int'>\n",
"52\n",
"Pitch: 52, Type: <class 'int'>\n",
"59\n",
"Pitch: 59, Type: <class 'int'>\n",
"52\n",
"Pitch: 52, Type: <class 'int'>\n",
"64\n",
"Pitch: 64, Type: <class 'int'>\n",
"60\n",
"Pitch: 60, Type: <class 'int'>\n",
"48\n",
"Pitch: 48, Type: <class 'int'>\n",
"48\n",
"Pitch: 48, Type: <class 'int'>\n",
"60\n",
"Pitch: 60, Type: <class 'int'>\n",
"64\n",
"Pitch: 64, Type: <class 'int'>\n",
"60\n",
"Pitch: 60, Type: <class 'int'>\n"
]
}
],
"source": [
"from midiutil import MIDIFile\n",
"\n",
"\n",
"track = 0\n",
"channel = 0\n",
"time = 0\n",
"duration = 1\n",
"tempo = 90\n",
"volume = 100\n",
"\n",
"MyMIDI = MIDIFile(1)\n",
"MyMIDI.addTempo(track, time, tempo)\n",
"\n",
"quarter_note = 1\n",
"half_note = 2\n",
"\n",
"n = 16\n",
"\n",
"def find_pitch(v):\n",
" key = str(v).replace(' ', '').replace('[', '').replace(']', '')\n",
" return pitch_mapping[key]\n",
"\n",
"v = np.array([0,0,0])\n",
"for i in range(n):\n",
" v = step(v) \n",
" pitch = find_pitch(v) \n",
" print(pitch) \n",
" print(f\"Pitch: {pitch}, Type: {type(pitch)}\") \n",
" MyMIDI.addNote(track, channel, pitch=pitch, duration=quarter_note, time=i, volume=volume)\n",
" \n",
" MyMIDI.addNote(track, channel, pitch=pitch, duration=quarter_note, time=i, volume=volume)\n",
"\n",
"output_file_path = \"pleasework.mid\"\n",
"outf = open(output_file_path, 'wb')\n",
"MyMIDI.writeFile(outf)\n",
"outf.close()\n"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [],
"source": [
"import subprocess\n",
"\n",
"def convert_midi_to_wav(midi_path, wav_path, soundfont_path):\n",
" command = f'fluidsynth -ni {soundfont_path} {midi_path} -F {wav_path} -r 44100'\n",
" subprocess.run(command, shell=True)\n",
"\n",
"# Placeholder paths - you'll need to update these with actual paths\n",
"soundfont_path = 'path/to/your/soundfont.sf2'\n",
"wav_file_path = 'output.wav'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}