Class: State
Overview
The State class represents a combination of qubits’ superpositions. They are stored a 2**n-dimensional column vector where N is the numbers of qubits entangled. State is usually produce after the applications of gates to a number of qubits and is continually passed forward through the quantum circuit until a measurement is made. State can be operated on via matrix multiplication or measurement(partial or otherwise).
Constant Summary
Constants included from QuantumVector
Instance Attribute Summary collapse
-
#qubits ⇒ Object
readonly
Returns the value of attribute qubits.
-
#vector ⇒ Object
readonly
Returns the value of attribute vector.
Instance Method Summary collapse
-
#initialize(vector, *qubits) ⇒ State
constructor
Takes a column matrix representing N qubits and the corresponding superposition Values of the matrix must obey the normalization constraint Additional takes references to the actual qubits so that they can be updated in the future.
-
#measure ⇒ Object
Returns an array of bits representing the final state of all entangled qubits All qubits are written with their new state and all superposition information is lost.
-
#measure_partial(*qubit) ⇒ Object
Takes an array of qubits for which a measurement should be made Returns an array of bits representing the final state of the requested qubits All others qubits are written with a normalized state and all superposition information is lost.
Methods included from QuantumVector
Constructor Details
#initialize(vector, *qubits) ⇒ State
Takes a column matrix representing N qubits and the corresponding superposition Values of the matrix must obey the normalization constraint Additional takes references to the actual qubits so that they can be updated in the future
206 207 208 209 210 211 212 |
# File 'lib/quantum_ruby.rb', line 206 def initialize(vector, *qubits) @vector = vector column_vector? normalized? @qubits = qubits.flatten.tap { |i| i.each { |j| j.entangled = true } } end |
Instance Attribute Details
#qubits ⇒ Object (readonly)
Returns the value of attribute qubits.
199 200 201 |
# File 'lib/quantum_ruby.rb', line 199 def qubits @qubits end |
#vector ⇒ Object (readonly)
Returns the value of attribute vector.
198 199 200 |
# File 'lib/quantum_ruby.rb', line 198 def vector @vector end |
Instance Method Details
#measure ⇒ Object
Returns an array of bits representing the final state of all entangled qubits All qubits are written with their new state and all superposition information is lost
217 218 219 |
# File 'lib/quantum_ruby.rb', line 217 def measure measure_partial(@qubits) end |
#measure_partial(*qubit) ⇒ Object
Takes an array of qubits for which a measurement should be made Returns an array of bits representing the final state of the requested qubits All others qubits are written with a normalized state and all superposition information is lost
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 |
# File 'lib/quantum_ruby.rb', line 225 def measure_partial(*qubit) # find location of our desired qubit(s) qubit_ids = qubit.map { |i| @qubits.find_index { |j| j.hash == i .hash } }.sort # collect probabilities for qubit(s) states sub_result = @vector.to_a.flatten.each_with_index.group_by do |_probability, index| qubit_ids.map do |id| index.to_s(2).rjust(size, '0')[id] end.join end # calculate final probabilities for qubit(s) state probabilities = sub_result.sort.to_h.transform_values { |v| v.reduce(0) { |i, p| i + p[0].abs2 } }.values # determine 'winner' acc = 0 out = nil secret = rand probabilities.each_with_index do |probability, index| acc += probability if acc > secret out = index break end end # Renormalize squared_sum_mag = Math.sqrt(probabilities[out]) out = out.to_s(2).rjust(qubit.length, '0') new_state = sub_result.fetch(out).map { |i| i[0] / squared_sum_mag } # Update each qubit @qubits.each_with_index do |q, i| q.entangled = false if (index = qubit_ids.find_index(i)) q.send(:vector=, Array.new(2, 0).tap { |vector| vector[out[index].to_i] = 1 }) else q.send(:vector=, new_state) end end # State should no longer be used out.split('').map(&:to_i) end |