#include #include #include #include #include #include #include #include #include #include #include #include #include using Quaternion = std::array; Quaternion operator+(const Quaternion &a, const Quaternion &b) { return {a[0] + b[0], a[1] + b[1], a[2] + b[2], a[3] + b[3]}; } Quaternion operator*(const Quaternion &a, const Quaternion &b) { return { a[0] * b[0] - a[1] * b[1] - a[2] * b[2] - a[3] * b[3], a[0] * b[1] + a[1] * b[0] + a[2] * b[3] - a[3] * b[2], a[0] * b[2] - a[1] * b[3] + a[2] * b[0] + a[3] * b[1], a[0] * b[3] + a[1] * b[2] - a[2] * b[1] + a[3] * b[0] }; } Quaternion conjugate(const Quaternion &a) { return {a[0], -a[1], -a[2], -a[3]}; } Quaternion scaled(const Quaternion &a, double scale) { return {a[0] * scale, a[1] * scale, a[2] * scale, a[3] * scale}; } double norm(const Quaternion &a) { return std::sqrt(std::inner_product(a.begin(), a.end(), a.begin(), 0.0)); } class Random { public: explicit Random(std::uint64_t seed) { for (auto &word : state) { seed += 0x9e3779b97f4a7c15ULL; std::uint64_t z = seed; z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL; z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL; word = z ^ (z >> 31); } } double uniform() { return static_cast(next() >> 11) * 0x1.0p-53; } double open() { return (static_cast(next() >> 11) + 0.5) * 0x1.0p-53; } private: std::array state{}; static std::uint64_t rotate(std::uint64_t x, int bits) { return (x << bits) | (x >> (64 - bits)); } std::uint64_t next() { const std::uint64_t result = rotate(state[1] * 5, 7) * 9; const std::uint64_t t = state[1] << 17; state[2] ^= state[0]; state[3] ^= state[1]; state[1] ^= state[2]; state[0] ^= state[3]; state[2] ^= t; state[3] = rotate(state[3], 45); return result; } }; class Lattice { public: Lattice(int size, double beta, std::uint64_t seed, bool cold) : size(size), beta(beta), volume(size * size * size * size), random(seed), links(volume * 4), plus(volume * 4), minus(volume * 4) { for (int x = 0; x < volume; ++x) { for (int mu = 0; mu < 4; ++mu) { links[index(x, mu)] = cold ? Quaternion{1, 0, 0, 0} : random_su2(); plus[index(x, mu)] = shift(x, mu, 1); minus[index(x, mu)] = shift(x, mu, -1); } } } void sweep() { for (int x = 0; x < volume; ++x) { for (int mu = 0; mu < 4; ++mu) { update(x, mu); } } } double max_link_norm_error() const { double result = 0; for (const Quaternion &link : links) { result = std::max(result, std::abs(norm(link) - 1)); } return result; } double average_plaquette() const { double total = 0; for (int x = 0; x < volume; ++x) { for (int mu = 0; mu < 4; ++mu) { for (int nu = mu + 1; nu < 4; ++nu) { const Quaternion p = links[index(x, mu)] * links[index(plus[index(x, mu)], nu)] * conjugate(links[index(plus[index(x, nu)], mu)]) * conjugate(links[index(x, nu)]); total += p[0]; } } } return total / (6.0 * volume); } Quaternion sample(double coupling) { if (coupling < 1e-12) { return random_su2(); } const double tail = std::exp(-2 * coupling); double a0; do { const double u = random.open(); a0 = 1 + std::log(tail + u * (1 - tail)) / coupling; } while (random.uniform() > std::sqrt(std::max(0.0, 1 - a0 * a0))); const double z = 2 * random.uniform() - 1; const double phi = 2 * pi * random.uniform(); const double circle_radius = std::sqrt(std::max(0.0, 1 - z * z)); const double vector_radius = std::sqrt(std::max(0.0, 1 - a0 * a0)); return {a0, vector_radius * circle_radius * std::cos(phi), vector_radius * circle_radius * std::sin(phi), vector_radius * z}; } private: static constexpr double pi = 3.141592653589793238462643383279502884; int size; double beta; int volume; Random random; std::vector links; std::vector plus; std::vector minus; int index(int x, int mu) const { return 4 * x + mu; } int shift(int x, int mu, int amount) const { std::array coordinates{}; int remainder = x; for (int direction = 0; direction < 4; ++direction) { coordinates[direction] = remainder % size; remainder /= size; } coordinates[mu] = (coordinates[mu] + amount + size) % size; int result = 0; for (int direction = 3; direction >= 0; --direction) { result = size * result + coordinates[direction]; } return result; } Quaternion random_su2() { const double a0 = 2 * random.uniform() - 1; const double z = 2 * random.uniform() - 1; const double phi = 2 * pi * random.uniform(); const double vector_radius = std::sqrt(std::max(0.0, 1 - a0 * a0)); const double circle_radius = std::sqrt(std::max(0.0, 1 - z * z)); return {a0, vector_radius * circle_radius * std::cos(phi), vector_radius * circle_radius * std::sin(phi), vector_radius * z}; } Quaternion staple(int x, int mu) const { Quaternion sum{0, 0, 0, 0}; for (int nu = 0; nu < 4; ++nu) { if (nu == mu) { continue; } const int x_plus_mu = plus[index(x, mu)]; const int x_plus_nu = plus[index(x, nu)]; const Quaternion forward = links[index(x_plus_mu, nu)] * conjugate(links[index(x_plus_nu, mu)]) * conjugate(links[index(x, nu)]); const int x_minus_nu = minus[index(x, nu)]; const int x_minus_nu_plus_mu = plus[index(x_minus_nu, mu)]; const Quaternion backward = conjugate(links[index(x_minus_nu_plus_mu, nu)]) * conjugate(links[index(x_minus_nu, mu)]) * links[index(x_minus_nu, nu)]; sum = sum + forward + backward; } return sum; } void update(int x, int mu) { const Quaternion s = staple(x, mu); const double magnitude = norm(s); if (magnitude < 1e-14) { links[index(x, mu)] = random_su2(); return; } const Quaternion a = sample(beta * magnitude); links[index(x, mu)] = a * scaled(conjugate(s), 1 / magnitude); } }; struct Statistics { double mean; double naive_error; double autocorrelation_error; double integrated_time; double blocking_error; int block_size; }; Statistics analyze(const std::vector &values) { const int n = static_cast(values.size()); const double mean = std::accumulate(values.begin(), values.end(), 0.0) / n; double variance = 0; for (double value : values) { variance += (value - mean) * (value - mean); } variance /= n - 1; const double naive_error = std::sqrt(variance / n); double integrated_time = 0.5; const int max_lag = std::min(2000, n / 10); int lag = 1; for (; lag <= max_lag; ++lag) { double covariance = 0; for (int i = 0; i + lag < n; ++i) { covariance += (values[i] - mean) * (values[i + lag] - mean); } covariance /= (n - lag) * variance; integrated_time += covariance; if (lag >= 5 * integrated_time) { break; } } integrated_time = std::max(0.5, integrated_time); const double autocorrelation_error = std::sqrt(2 * integrated_time * variance / n); const int block_size = std::max(1, static_cast(std::ceil(40 * integrated_time))); const int blocks = n / block_size; double blocking_error = naive_error; if (blocks >= 2) { std::vector means(blocks, 0); for (int block = 0; block < blocks; ++block) { means[block] = std::accumulate(values.begin() + block * block_size, values.begin() + (block + 1) * block_size, 0.0) / block_size; } const double block_mean = std::accumulate(means.begin(), means.end(), 0.0) / blocks; double block_variance = 0; for (double value : means) { block_variance += (value - block_mean) * (value - block_mean); } block_variance /= blocks - 1; blocking_error = std::sqrt(block_variance / blocks); } return {mean, naive_error, autocorrelation_error, integrated_time, blocking_error, block_size}; } std::uint64_t parse_seed(const std::string &text) { return std::stoull(text); } int parse_int(const std::string &text) { return std::stoi(text); } int main(int argc, char **argv) { int size = 8; double beta = 2.2986; std::uint64_t seed = 20261202; int thermalization = 2000; int sweeps = 20000; bool cold = true; std::string history_path; for (int i = 1; i < argc; ++i) { const std::string argument = argv[i]; if (i + 1 >= argc) { std::cerr << "missing value after " << argument << '\n'; return 2; } const std::string value = argv[++i]; if (argument == "--size") size = parse_int(value); else if (argument == "--beta") beta = std::stod(value); else if (argument == "--seed") seed = parse_seed(value); else if (argument == "--thermalization") thermalization = parse_int(value); else if (argument == "--sweeps") sweeps = parse_int(value); else if (argument == "--start") cold = value == "cold"; else if (argument == "--history") history_path = value; else { std::cerr << "unknown option: " << argument << '\n'; return 2; } } if (size < 2 || beta < 0 || thermalization < 0 || sweeps < 2) { std::cerr << "require size >= 2, beta >= 0, thermalization >= 0 and sweeps >= 2\n"; return 2; } if (!cold) { bool valid_start = false; for (int i = 1; i + 1 < argc; ++i) { if (std::string(argv[i]) == "--start") { valid_start = std::string(argv[i + 1]) == "hot"; } } if (!valid_start) { std::cerr << "--start must be cold or hot\n"; return 2; } } const auto started = std::chrono::steady_clock::now(); Lattice lattice(size, beta, seed, cold); const double initial_plaquette = lattice.average_plaquette(); for (int sweep = 0; sweep < thermalization; ++sweep) { lattice.sweep(); } const double thermalized_plaquette = lattice.average_plaquette(); std::vector values; values.reserve(sweeps); std::ofstream history; if (!history_path.empty()) { history.open(history_path); history << std::setprecision(17); } for (int sweep = 0; sweep < sweeps; ++sweep) { lattice.sweep(); const double value = lattice.average_plaquette(); values.push_back(value); if (history.is_open()) { history << sweep + 1 << ' ' << value << '\n'; } } const Statistics statistics = analyze(values); const double error = std::max({statistics.naive_error, statistics.autocorrelation_error, statistics.blocking_error}); const double seconds = std::chrono::duration(std::chrono::steady_clock::now() - started).count(); std::cout << std::setprecision(17) << "{\n" << " \"group\": \"SU(2)\",\n" << " \"beta\": " << beta << ",\n" << " \"lattice\": [" << size << ", " << size << ", " << size << ", " << size << "],\n" << " \"start\": \"" << (cold ? "cold" : "hot") << "\",\n" << " \"seed\": " << seed << ",\n" << " \"thermalization_sweeps\": " << thermalization << ",\n" << " \"measurement_sweeps\": " << sweeps << ",\n" << " \"initial_plaquette\": " << initial_plaquette << ",\n" << " \"thermalized_plaquette\": " << thermalized_plaquette << ",\n" << " \"max_link_norm_error\": " << lattice.max_link_norm_error() << ",\n" << " \"plaquette\": " << statistics.mean << ",\n" << " \"error\": " << error << ",\n" << " \"naive_error\": " << statistics.naive_error << ",\n" << " \"autocorrelation_error\": " << statistics.autocorrelation_error << ",\n" << " \"integrated_autocorrelation_time\": " << statistics.integrated_time << ",\n" << " \"blocking_error\": " << statistics.blocking_error << ",\n" << " \"block_size\": " << statistics.block_size << ",\n" << " \"seconds\": " << seconds << "\n" << "}\n"; }