1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <iostream> #include <complex> #include <iomanip> #include <cstdlib> #include <ctime> const double PI = 3.141592653589793; // A complex number type for quantum amplitudes using Complex = std::complex<double>; // Class to represent a Qubit class Qubit { public: Qubit(double theta) { // Initialize the qubit state with angles theta and phi alpha = std::cos(theta / 2); beta = std::sin(theta / 2) * std::exp(Complex(0, 2 * PI * std::rand() / RAND_MAX)); } // Print the state of the qubit void printState() const { std::cout << "Qubit State: "; std::cout << std::fixed << std::setprecision(2) << "|" << alpha << "|0> + " << beta << "|1>" << std::endl; } // Measure the qubit state int measure() const { // Compute probabilities double prob0 = std::norm(alpha); double prob1 = std::norm(beta); // Print probabilities std::cout << "Probability of |0>: " << prob0 << std::endl; std::cout << "Probability of |1>: " << prob1 << std::endl; // Randomly choose the measurement outcome based on probabilities double rand_val = static_cast<double>(std::rand()) / RAND_MAX; return rand_val < prob0 ? 0 : 1; } private: Complex alpha; // Amplitude for |0> Complex beta; // Amplitude for |1> }; int main() { // Seed the random number generator std::srand(static_cast<unsigned>(std::time(nullptr))); double theta; std::cout << "Enter the angle theta (in radians) for the qubit state: "; std::cin >> theta; Qubit qubit(theta); qubit.printState(); int measurement = qubit.measure(); std::cout << "Measurement result: |" << measurement << ">" << std::endl; return 0; } |
Explanation
- Constants:
- PI: Mathematical constant π used for generating random angles.
- Type Alias:
- Complex: Alias for
std::complex<double>
to represent complex numbers for quantum amplitudes.
- Complex: Alias for
- Class
Qubit
:- Purpose: Represents a quantum bit (qubit) and provides functionality to initialize, print its state, and measure it.
- Constructor
Qubit(double theta)
:- Initializes the qubit state with a given angle
theta
. - Alpha: Amplitude for the
|0>
state. - Beta: Amplitude for the
|1>
state, with a random phase.
- Initializes the qubit state with a given angle
- Method
printState()
:- Prints the current state of the qubit in the form
α|0> + β|1>
.
- Prints the current state of the qubit in the form
- Method
measure()
:- Computes probabilities of measuring the qubit in the
|0>
or|1>
states. - Prints the probabilities.
- Simulates a measurement by randomly choosing between
|0>
and|1>
based on these probabilities.
- Computes probabilities of measuring the qubit in the
- Main Function:
- Setup: Seeds the random number generator for measurement simulations.
- User Input: Prompts the user to enter an angle
theta
to initialize the qubit. - Simulation:
- Initializes a
Qubit
instance with the given angle. - Prints the qubit’s state.
- Performs a measurement and displays the result.
- Initializes a
Usage
- Quantum State Representation: Demonstrates how a qubit can be initialized in a superposition state.
- Measurement Simulation: Simulates the process of measuring a qubit and collapsing its state based on quantum probabilities.