Class: Qubit
Overview
The Qubit class represents quantum bit. They are stored a 2-dimensional column vector. Qubits can be operated on via matrix multiplication or measurement.
Constant Summary collapse
- ENTANGLED_WARNING =
"Alert: This qubit is entangled.\n\tPlease locate and measure the State\n\tthat contains this qubit's superposition.".freeze
Constants included from QuantumVector
Instance Attribute Summary collapse
-
#entangled ⇒ Object
Returns the value of attribute entangled.
-
#vector ⇒ Object
readonly
Returns the value of attribute vector.
Instance Method Summary collapse
-
#initialize(zero, one) ⇒ Qubit
constructor
Takes two values for the superposition of |0> and |1> respectively and store it in a Qubit Values must obey the normalization constraint.
-
#measure ⇒ Object
Measurement can only happen at the expense of information elsewhere in the system.
-
#state ⇒ Object
Return the qubit’s superposition as a pretty printed array Qubit.news(1, 0).state # [1 # 0].
-
#to_s ⇒ Object
Returns the qubit’s superposition in Bra-Ket notation Qubit.news(1, 0).to_s # 1|0> Qubit.news(0, 1).to_s # 1|1> Qubit.new(1/Math.sqrt(2), 1/Math.sqrt(2)) # 0.707|0> + 0.707|1>.
Methods included from QuantumVector
Constructor Details
#initialize(zero, one) ⇒ Qubit
Takes two values for the superposition of |0> and |1> respectively and store it in a Qubit Values must obey the normalization constraint
120 121 122 123 |
# File 'lib/quantum_ruby.rb', line 120 def initialize(zero, one) @vector = Matrix[[zero], [one]] normalized? end |
Instance Attribute Details
#entangled ⇒ Object
Returns the value of attribute entangled.
111 112 113 |
# File 'lib/quantum_ruby.rb', line 111 def entangled @entangled end |
#vector ⇒ Object
Returns the value of attribute vector.
110 111 112 |
# File 'lib/quantum_ruby.rb', line 110 def vector @vector end |
Instance Method Details
#measure ⇒ Object
Measurement can only happen at the expense of information elsewhere in the system. If two qubits are combined then their combined state becomes dependent so doing something to one can affect the other. This applies both to further operations or measurement. After measurement all related qubits’ superpositions collapse to a classical interpretation, either 0 or 1
Qubit.news(1, 0).to_s # 0, qubit remains the same
Qubit.news(0, 1).to_s # 1, qubit remains the same
Qubit.new(1/Math.sqrt(2), 1/Math.sqrt(2)) # either 0 or 1 and qubit superposition collapses
to look like one the above qubits depending on the outcome
134 135 136 137 138 139 140 141 142 |
# File 'lib/quantum_ruby.rb', line 134 def measure if rand < @vector[0, 0].abs2 @vector = [1, 0] 0 else @vector = [0, 1] 1 end end |
#state ⇒ Object
Return the qubit’s superposition as a pretty printed array
Qubit.news(1, 0).state
# [1
# 0]
149 150 151 152 153 |
# File 'lib/quantum_ruby.rb', line 149 def state return if entangled? super end |
#to_s ⇒ Object
Returns the qubit’s superposition in Bra-Ket notation
Qubit.news(1, 0).to_s # 1|0>
Qubit.news(0, 1).to_s # 1|1>
Qubit.new(1/Math.sqrt(2), 1/Math.sqrt(2)) # 0.707|0> + 0.707|1>
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/quantum_ruby.rb', line 160 def to_s return if entangled? first = el(0) last = el(1) out = '' out << first.to_s << ZERO_KET unless first.zero? out << ADD_SYM unless first.zero? || last.zero? out << last.to_s << ONE_KET unless last.zero? out end |