Class: Gate

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

Overview

X_GATE Y_GATE Z_GATE H_GATE T_GATE C_NOT_GATE SWAP_GATE TOFFOLI_GATE

Instance Method Summary collapse

Methods inherited from Matrix

#kronecker

Instance Method Details

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

Applies the ‘effect’ of the gate on the arguments. The matrix operation on a gate can take multiple arguments but the dimensions must match according to the general rules of matrix multiplication.

Gates can be scaled if you want to only affect less than N qubits of a system. Scaling occurs either up or down. If both is desired choose one direction and manually scale the other direction. NOTE scaling is done brute force and may no be applicable to the needs of your circuit. Refer to scaling examples for more details: github.com/AlessandroMinali/exanples/auto_scaling_gates_examples.rb

Can take Gate, Qubit, or State Results in a new Gate if supplied a Gate, otherwise returns a new State



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/quantum_ruby.rb', line 306

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