Class: Gate

Inherits:
Matrix show all
Defined in:
lib/quantum_ruby.rb

Instance Method Summary collapse

Methods inherited from Matrix

#kronecker

Instance Method Details

#*(*args, scale: nil) ⇒ Object

can take gate, qubit, or state



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/quantum_ruby.rb', line 258

def *(*args, scale: nil)
  if scale
    arg = begin
            args[0].vector
          rescue StandardError
            args[0]
          end
    diff = (arg.row_count / row_count)
    if diff > 1
      return case scale
             when :down
               Gate[*kronecker(Matrix.identity(diff))].*(*args)
             when :up
               Gate[*Matrix.identity(diff).kronecker(self)].*(*args)
             end
    end
  end

  case args[0]
  when State, Qubit
    qubits = []
    args = args.map do |i|
      qubits << (i.is_a?(Qubit) ? i : i.qubits)
      i.vector
    end.reduce(:kronecker)
    State.new(super(args), qubits)
  else
    super(*args)
  end
end