Site Tools


cuda-q-state-preparation

Table of Contents

State Preparation

State preparation initializes qubits to desired quantum states. All qubits start in $|0\rangle$; use gates to prepare superpositions, entangled states, or arbitrary states.

#include "cudaq.h"
#include <cmath>
 
// Prepare equal superposition
struct EqualSuperposition {
  void operator()(int n_qubits) __qpu__ {
    cudaq::qvector q(n_qubits);
    for (int i = 0; i < n_qubits; i++) {
      h(q[i]);  // Hadamard: |0⟩ → (|0⟩ + |1⟩)/√2
    }
    mz(q);
  }
};
 
// Prepare Bell state
struct BellState {
  void operator()() __qpu__ {
    cudaq::qvector q(2);
    h(q[0]);
    cx(q[0], q[1]);
    mz(q);  // (|00⟩ + |11⟩)/√2
  }
};
 
// Prepare GHZ state (n qubits)
struct GHZState {
  void operator()(int n) __qpu__ {
    cudaq::qvector q(n);
    h(q[0]);
    for (int i = 1; i < n; i++) {
      cx(q[i-1], q[i]);  // (|00...0⟩ + |11...1⟩)/√2
    }
    mz(q);
  }
};
 
// Arbitrary state preparation via rotation angles
struct ArbitraryState {
  void operator()(std::vector<double> angles) __qpu__ {
    int n = angles.size();
    cudaq::qvector q(n);
 
    for (int i = 0; i < n; i++) {
      ry(angles[i], q[i]);
      rz(angles[i], q[i]);  // General single-qubit rotation
    }
 
    mz(q);
  }
};
 
int main() {
  // Execute state preparations
  auto equal = cudaq::sample<EqualSuperposition>(1000, 3);
  auto bell = cudaq::sample<BellState>(1000);
  auto ghz = cudaq::sample<GHZState>(1000, 5);
 
  return 0;
}

State preparation is the first step in any quantum algorithm. The quality of preparation affects algorithm success—imperfect preparation introduces errors.

cuda-q-state-preparation.md · Last modified: by 127.0.0.1