Class TwoQubitGate

java.lang.Object
com.quantum4j.core.gates.TwoQubitGate
All Implemented Interfaces:
Gate
Direct Known Subclasses:
CHGate, CNOTGate, CZGate, ISWAPGate, StandardGates.CHGate, StandardGates.CNOTGate, StandardGates.CZGate, StandardGates.ISWAPGate, StandardGates.SWAPGate, SWAPGate

public abstract class TwoQubitGate extends Object implements Gate
Abstract base class for two-qubit gates. A two-qubit gate is represented by a 4×4 complex matrix in the local basis: |q0 q1⟩ = |00⟩, |01⟩, |10⟩, |11⟩ where q0 is the FIRST qubit argument passed to apply(StateVector, int, int) and q1 is the SECOND. Quantum4J uses LSB = qubit 0 in the global state index, but this class does not sort or reorder the qubit indices – the matrix is always applied with respect to the (q0, q1) order given by the caller. This fixes: - CNOT semantics: control = first argument, target = second - CH / other asymmetric 2-qubit gates - QASM Bell state tests
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Complex[][]
    4×4 unitary matrix in local ordering |q0 q1⟩ = |00⟩,|01⟩,|10⟩,|11⟩.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    TwoQubitGate(Complex[][] matrix)
    Construct a two-qubit gate with the given 4×4 matrix.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    apply(StateVector state, int q0, int q1)
    Apply this gate to qubits (q0, q1) in that logical order.
    int
    Get the arity (number of qubits) this gate acts upon.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.quantum4j.core.gates.Gate

    name
  • Field Details

    • matrix

      protected final Complex[][] matrix
      4×4 unitary matrix in local ordering |q0 q1⟩ = |00⟩,|01⟩,|10⟩,|11⟩.
  • Constructor Details

    • TwoQubitGate

      protected TwoQubitGate(Complex[][] matrix)
      Construct a two-qubit gate with the given 4×4 matrix.
      Parameters:
      matrix - a 4×4 complex matrix
  • Method Details

    • arity

      public int arity()
      Description copied from interface: Gate
      Get the arity (number of qubits) this gate acts upon.
      Specified by:
      arity in interface Gate
      Returns:
      1 for single-qubit gates, 2 for two-qubit gates, etc.
    • apply

      public void apply(StateVector state, int q0, int q1)
      Apply this gate to qubits (q0, q1) in that logical order. Local basis ordering: index 0 → |q0=0, q1=0⟩ index 1 → |q0=0, q1=1⟩ index 2 → |q0=1, q1=0⟩ index 3 → |q0=1, q1=1⟩