From bc515d3acdd94847b6e7aa6135bc234b46161db6 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Tue, 7 May 2024 07:00:43 -0400 Subject: add hw9 and hw8 --- hw9/12-11-all.png | Bin 0 -> 50155 bytes hw9/12-11-all.svg | 131 ++++++++++++++++++++++ hw9/12-11.jl | 272 +++++++++++++++++++++++++++++++++++++++++++++ hw9/12-12-all.png | Bin 0 -> 32142 bytes hw9/12-12-test.png | Bin 0 -> 29601 bytes hw9/12-12.jl | 253 +++++++++++++++++++++++++++++++++++++++++ hw9/YaoQuantumComputing.jl | 28 +++++ hw9/qubit00.svg | 39 +++++++ hw9/qubit01.svg | 39 +++++++ hw9/qubit10.svg | 39 +++++++ hw9/qubit11.svg | 39 +++++++ hw9/yao-assigment-2.jl | 75 +++++++++++++ 12 files changed, 915 insertions(+) create mode 100644 hw9/12-11-all.png create mode 100644 hw9/12-11-all.svg create mode 100644 hw9/12-11.jl create mode 100644 hw9/12-12-all.png create mode 100644 hw9/12-12-test.png create mode 100644 hw9/12-12.jl create mode 100644 hw9/YaoQuantumComputing.jl create mode 100644 hw9/qubit00.svg create mode 100644 hw9/qubit01.svg create mode 100644 hw9/qubit10.svg create mode 100644 hw9/qubit11.svg create mode 100644 hw9/yao-assigment-2.jl (limited to 'hw9') diff --git a/hw9/12-11-all.png b/hw9/12-11-all.png new file mode 100644 index 0000000..ac5589d Binary files /dev/null and b/hw9/12-11-all.png differ diff --git a/hw9/12-11-all.svg b/hw9/12-11-all.svg new file mode 100644 index 0000000..c6bc3c6 --- /dev/null +++ b/hw9/12-11-all.svg @@ -0,0 +1,131 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw9/12-11.jl b/hw9/12-11.jl new file mode 100644 index 0000000..ee7e2b1 --- /dev/null +++ b/hw9/12-11.jl @@ -0,0 +1,272 @@ +# my neural net from 12.11 + +# first, store the memory of A as a lattice of 1, -1, where 1 is the memory and -1 is the absence of memory +A = [ + -1 -1 -1 -1 1 1 -1 -1 -1 -1; + -1 -1 -1 1 1 1 1 -1 -1 -1; + -1 -1 1 1 -1 -1 1 1 -1 -1; + -1 1 1 -1 -1 -1 -1 1 1 -1; + 1 1 -1 -1 -1 -1 -1 -1 1 1; + 1 1 -1 -1 -1 -1 -1 -1 1 1; + -1 1 1 -1 -1 -1 -1 1 1 -1; + -1 -1 1 1 -1 -1 1 1 -1 -1; + -1 -1 -1 1 1 1 1 -1 -1 -1; + -1 -1 -1 -1 1 1 -1 -1 -1 -1 +] + +B = [ + 1 1 1 1 1 -1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 1 1 1 -1 -1 -1 -1 -1; + 1 1 1 1 1 -1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 1 1 1 -1 -1 -1 -1 -1 +] + +C = [ + -1 1 1 1 1 1 1 -1 -1 -1; + 1 1 1 1 1 1 1 -1 -1 -1; + 1 1 1 -1 -1 -1 -1 -1 -1 -1; + 1 1 -1 -1 -1 -1 -1 -1 -1 -1; + 1 1 -1 -1 -1 -1 -1 -1 -1 -1; + 1 1 -1 -1 -1 -1 -1 -1 -1 -1; + 1 1 -1 -1 -1 -1 -1 -1 -1 -1; + 1 1 1 -1 -1 -1 -1 -1 -1 -1; + 1 1 1 1 1 1 1 -1 -1 -1; + -1 1 1 1 1 1 1 -1 -1 -1 +] + +D = [ + 1 1 1 1 1 -1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -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+ -1 -1 1 1 1 1 1 1 1 1 +] + +encodings = [A, B, F, G] + +spin_numbers(row, col) = (row - 1) * 10 + col + +# create strength of interactions between ith and jth spinds in Ji,j +J = zeros(100, 100) +for m in 1:100 # row + for n in 1:100 # col + i = (m - 1) % 10 + 1 + j = (n - 1) % 10 + 1 + k = (m - 1) ÷ 10 + 1 + l = (n - 1) ÷ 10 + 1 + + J[m, n] = 0 + for encoding in encodings + J[m, n] += encoding[i, k] * encoding[j, l] + end + J[m, n] /= length(encodings) + end +end + +# the energy function +function energy(s, J) + E = 0 + for i in 1:100 + for j in 1:100 + E += J[i, j] * s[i] * s[j] + end + end + return -E +end + +# the update function (uses monte carlo steps) +function monte_carlo(s, J) + for i in 1:100 # systematically go through each point in the lattice + # calculate the energy of the system + E = energy(s, J) + # randomly flip a spin + s[i] = -s[i] + # calculate the new energy of the system + E_new = energy(s, J) + # calculate the change in energy + dE = E_new - E + # if the change in energy is positive, flip the spin back + if dE > 0 + s[i] = -s[i] + end + end + return s +end + +# create the main function +function main(s, J, nsteps) + E = energy(s, J) + for i in 1:nsteps + s = monte_carlo(s, J) + E = energy(s, J) + end + return s, E +end + +# run the main function + +# randomly change some values in A to see if NN works +function produce_test_arr(enc, prob_change = 0.1) + tmp = zeros(10, 10) + for i in 1:10 + for j in 1:10 + if rand() < prob_change + tmp[i, j] = rand([-1, 1]) # set some random values + else + tmp[i, j] = enc[i, j] + end + end + end + return tmp +end + +function check_if_same(s, enc) + for i in 1:10 + for j in 1:10 + if s[i, j] != enc[i, j] + println("The neural net did not work") + return false + end + end + end + # println("The neural net worked") + return true +end + +function apply_damage_to_J(J, prob_damage = 0.8) + println("Applying damage to J with probability ", prob_damage) + ret = copy(J) + for i in 1:100 + for j in 1:100 + if rand() < prob_damage + ret[i, j] = 0 + else + ret[i, j] = J[i, j] + end + end + end + return ret +end + +function run_tests(num_times, Js, MC_steps = 2) + num_correct = 0 + num_total = 0 + + for i in 1:num_times + # randomly select a memory + enc = encodings[rand(1:length(encodings))] + # produce a test array + s = produce_test_arr(enc) + # run theNN + s, E = main(s, Js, MC_steps) + # check if the NN worked + if check_if_same(s, enc) + num_correct += 1 + end + num_total += 1 + end + + println("Number of correct tests: ", num_correct) + + return num_correct / num_total +end + +function run_tests_for_damage_range(damages, num_times, MC_steps = 2, init_J = J) + res = [] + + for damage in damages + damaged_J = apply_damage_to_J(init_J, damage) + p = run_tests(num_times, damaged_J, MC_steps) + + push!(res, p) + end + + return res +end + + +# J = apply_damage_to_J(J, 0.8) + +# s = produce_test_arr(F) +# s, E = main(s, J, 2) +# check_if_same(s, F) + +s = produce_test_arr(A) +s, E = main(s, J, 2) +check_if_same(s, A) + +# s = produce_test_arr(C, 0.0) +# s, E = main(s, J, 2) +# check_if_same(s, C) + +damage_range = collect(0.1:0.05:1.0) +res = run_tests_for_damage_range(damage_range, 100) + +#plot the results +using Plots +plot(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 2") + +# do the same for different number of MC steps +res = run_tests_for_damage_range(damage_range, 100, 5) +plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 5") + +res = run_tests_for_damage_range(damage_range, 100, 10) +plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 10") + +res = run_tests_for_damage_range(damage_range, 100, 20) +plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 20") + +savefig("12-11-all.png") + + + diff --git a/hw9/12-12-all.png b/hw9/12-12-all.png new file mode 100644 index 0000000..edfb678 Binary files /dev/null and b/hw9/12-12-all.png differ diff --git a/hw9/12-12-test.png b/hw9/12-12-test.png new file mode 100644 index 0000000..f86297e Binary files /dev/null and b/hw9/12-12-test.png differ diff --git a/hw9/12-12.jl b/hw9/12-12.jl new file mode 100644 index 0000000..e08eadc --- /dev/null +++ b/hw9/12-12.jl @@ -0,0 +1,253 @@ +# my neural net from 12.11 + +# first, store the memory of A as a lattice of 1, -1, where 1 is the memory and -1 is the absence of memory +A = [ + -1 -1 -1 -1 1 1 -1 -1 -1 -1; + -1 -1 -1 1 1 1 1 -1 -1 -1; + -1 -1 1 1 -1 -1 1 1 -1 -1; + -1 1 1 -1 -1 -1 -1 1 1 -1; + 1 1 -1 -1 -1 -1 -1 -1 1 1; + 1 1 -1 -1 -1 -1 -1 -1 1 1; + -1 1 1 -1 -1 -1 -1 1 1 -1; + -1 -1 1 1 -1 -1 1 1 -1 -1; + -1 -1 -1 1 1 1 1 -1 -1 -1; + -1 -1 -1 -1 1 1 -1 -1 -1 -1 +] + +B = [ + 1 1 1 1 1 -1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 1 1 1 -1 -1 -1 -1 -1; + 1 1 1 1 1 -1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 1 1 1 -1 -1 -1 -1 -1 +] + +C = [ + -1 1 1 1 1 1 1 -1 -1 -1; + 1 1 1 1 1 1 1 -1 -1 -1; + 1 1 1 -1 -1 -1 -1 -1 -1 -1; + 1 1 -1 -1 -1 -1 -1 -1 -1 -1; + 1 1 -1 -1 -1 -1 -1 -1 -1 -1; + 1 1 -1 -1 -1 -1 -1 -1 -1 -1; + 1 1 -1 -1 -1 -1 -1 -1 -1 -1; + 1 1 1 -1 -1 -1 -1 -1 -1 -1; + 1 1 1 1 1 1 1 -1 -1 -1; + -1 1 1 1 1 1 1 -1 -1 -1 +] + +D = [ + 1 1 1 1 1 -1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -1; + 1 1 -1 -1 1 1 -1 -1 -1 -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 -1 1 1 1 1 1 1 1 1 +] + +encodings = [A, B, C, D, E_letter, F, G] + +spin_numbers(row, col) = (row - 1) * 10 + col + +# create strength of interactions between ith and jth spinds in Ji,j +J = zeros(100, 100) +for m in 1:100 # row + for n in 1:100 # col + i = (m - 1) % 10 + 1 + j = (n - 1) % 10 + 1 + k = (m - 1) ÷ 10 + 1 + l = (n - 1) ÷ 10 + 1 + + J[m, n] = 0 + for encoding in encodings + J[m, n] += encoding[i, k] * encoding[j, l] + end + J[m, n] /= length(encodings) + end +end + +# the energy function +function energy(s, J) + E = 0 + for i in 1:100 + for j in 1:100 + E += J[i, j] * s[i] * s[j] + end + end + return -E +end + +# the update function (uses monte carlo steps) +function monte_carlo(s, J) + for i in 1:100 # systematically go through each point in the lattice + # calculate the energy of the system + E = energy(s, J) + # randomly flip a spin + s[i] = -s[i] + # calculate the new energy of the system + E_new = energy(s, J) + # calculate the change in energy + dE = E_new - E + # if the change in energy is positive, flip the spin back + if dE > 0 + s[i] = -s[i] + end + end + return s +end + +# create the main function +function main(s, J, nsteps) + E = energy(s, J) + for i in 1:nsteps + s = monte_carlo(s, J) + E = energy(s, J) + end + return s, E +end + +# run the main function + +# randomly change some values in A to see if NN works +function produce_test_arr(enc, prob_change = 0.1) + tmp = zeros(10, 10) + for i in 1:10 + for j in 1:10 + if rand() < prob_change + tmp[i, j] = rand([-1, 1]) # set some random values + else + tmp[i, j] = enc[i, j] + end + end + end + return tmp +end + +function check_if_same(s, enc) + for i in 1:10 + for j in 1:10 + if s[i, j] != enc[i, j] + println("The neural net did not work") + return false + end + end + end + # println("The neural net worked") + return true +end + +function apply_damage_to_J(J, prob_damage = 0.8) + println("Applying damage to J with probability ", prob_damage) + ret = copy(J) + for i in 1:100 + for j in 1:100 + if rand() < prob_damage + ret[i, j] = 0 + else + ret[i, j] = J[i, j] + end + end + end + return ret +end + +function run_tests(num_times, Js, MC_steps = 2) + num_correct = 0 + num_total = 0 + + for i in 1:num_times + # randomly select a memory + enc = encodings[rand(1:length(encodings))] + # produce a test array + s = produce_test_arr(enc) + # run theNN + s, E = main(s, Js, MC_steps) + # check if the NN worked + if check_if_same(s, enc) + num_correct += 1 + end + num_total += 1 + end + + println("Number of correct tests: ", num_correct) + + return num_correct / num_total +end + +function run_tests_for_damage_range(damages, num_times, MC_steps = 2, init_J = J) + res = [] + + for damage in damages + damaged_J = apply_damage_to_J(init_J, damage) + p = run_tests(num_times, damaged_J, MC_steps) + + push!(res, p) + end + + return res +end + +damage_range = collect(0.0:0.1:0.15) +res = run_tests_for_damage_range(damage_range, 10) + +#plot the results +using Plots +plot(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 2") + +res = run_tests_for_damage_range(damage_range, 10, 10) +plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 10") + +res = run_tests_for_damage_range(damage_range, 10, 100) +plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 100") + +res = run_tests_for_damage_range(damage_range, 10, 1000) +plot!(damage_range, res, xlabel = "Damage", ylabel = "Success rate", title = "NN success rate vs damage probability", marker = :circle, label = "MC steps = 1000") + +savefig("12-12-all.png") \ No newline at end of file diff --git a/hw9/YaoQuantumComputing.jl b/hw9/YaoQuantumComputing.jl new file mode 100644 index 0000000..36c22d6 --- /dev/null +++ b/hw9/YaoQuantumComputing.jl @@ -0,0 +1,28 @@ +using Yao, YaoPlots + +let + circuit = chain(2, put(1=>X), put(2=>X)) + plot(circuit) +end + + +begin + bellcircuit = chain(2, put(1=>H), control(1, 2=>X)) + plot(bellcircuit) +end + + +q1 = ArrayReg(bit"00") #creating the system of two qubits with state |00> +println(state(q1)) +println(q1 |> r->measure(r, nshots=10)) + +a = (q1 |> bellcircuit) +println(state(a)) +println(a |> r->measure(r, nshots=10)) + + +reversebellcircuit = chain(2, control(1,2=>X), put(1=>H)) +plot(reversebellcircuit) + +b = (a |> reversebellcircuit) +println(state(b)) \ No newline at end of file diff --git a/hw9/qubit00.svg b/hw9/qubit00.svg new file mode 100644 index 0000000..677f7e7 --- /dev/null +++ b/hw9/qubit00.svg @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw9/qubit01.svg b/hw9/qubit01.svg new file mode 100644 index 0000000..e24d9f4 --- /dev/null +++ b/hw9/qubit01.svg @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw9/qubit10.svg b/hw9/qubit10.svg new file mode 100644 index 0000000..8dcead0 --- /dev/null +++ b/hw9/qubit10.svg @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw9/qubit11.svg b/hw9/qubit11.svg new file mode 100644 index 0000000..0833dcc --- /dev/null +++ b/hw9/qubit11.svg @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hw9/yao-assigment-2.jl b/hw9/yao-assigment-2.jl new file mode 100644 index 0000000..7de7987 --- /dev/null +++ b/hw9/yao-assigment-2.jl @@ -0,0 +1,75 @@ +begin + using Pkg + Pkg.activate(mktempdir()) + Pkg.Registry.update() + Pkg.add("Yao") + Pkg.add("YaoPlots") + Pkg.add("StatsBase") + Pkg.add("Plots") + Pkg.add("BitBasis") +end + +using Yao, YaoPlots + +# make the bell circuit +bellcircuit = chain(2, put(1 => H), control(1, 2 => X)) + +# make the reverse bell circuit +reversebellcircuit = chain(2, control(1, 2 => X), put(1 => H)) + +# circuit that takes two qubits and passes it through +# the bell circuit then the reverse bell circuit +circuit = chain(2, bellcircuit, reversebellcircuit) +# plot(circuit) + +# make the qubits |00>, |01>, |10>, |11> +qubit00 = ArrayReg(bit"00") +qubit10 = ArrayReg(bit"10") # circuit reads in reverse order +qubit01 = ArrayReg(bit"01") +qubit11 = ArrayReg(bit"11") + +using StatsBase: Histogram, fit +using Plots: bar, scatter!, gr; +gr(); +using BitBasis +function plotmeasure(x::Array{BitStr{n, Int}, 1}) where n + hist = fit(Histogram, Int.(x), 0:2^n) + x = 0 + if (n <= 3) + s = 8 + elseif (n > 3 && n <= 6) + s = 5 + elseif (n > 6 && n <= 10) + s = 3.2 + elseif (n > 10 && n <= 15) + s = 2 + elseif (n > 15) + s = 1 + end + bar(hist.edges[1] .- 0.5, hist.weights, legend = :none, size = (600 * (2^n) / s, 400), ylims = (0:maximum(hist.weights)), xlims = (0:2^n), grid = :false, ticks = false, border = :none, color = :lightblue, lc = :lightblue) + scatter!(0:2^n-1, ones(2^n, 1), markersize = 0, + series_annotations = "|" .* string.(hist.edges[1]; base = 2, pad = n) .* "⟩") + scatter!(0:2^n-1, zeros(2^n, 1) .+ maximum(hist.weights), markersize = 0, + series_annotations = string.(hist.weights)) +end + +# pass them through the circuit 1024 times, taking measurements +println("Qubit 00") +measured_qubits00 = qubit00 |> circuit -> measure(circuit, nshots = 1024) +println(measured_qubits00) +plotmeasure(measured_qubits00) + +println("Qubit 01") +measured_qubits01 = qubit01 |> circuit -> measure(circuit, nshots = 1024) +println(measured_qubits01) +plotmeasure(measured_qubits01) + +println("Qubit 10") +measured_qubits10 = qubit10 |> circuit -> measure(circuit, nshots = 1024) +println(measured_qubits10) +plotmeasure(measured_qubits10) + +println("Qubit 11") +measured_qubits11 = qubit11 |> circuit -> measure(circuit, nshots = 1024) +println(measured_qubits11) +plotmeasure(measured_qubits11) -- cgit v1.2.3-70-g09d2