|
| 1 | +// Copyright 2025 Google LLC. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <unistd.h> |
| 16 | + |
| 17 | +#include <algorithm> |
| 18 | +#include <complex> |
| 19 | +#include <limits> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include "../lib/circuit_qsim_parser.h" |
| 23 | +#include "../lib/formux.h" |
| 24 | +#include "../lib/gates_qsim.h" |
| 25 | +#include "../lib/io_file.h" |
| 26 | +#include "../lib/multiprocess_custatevecex.h" |
| 27 | +#include "../lib/run_custatevecex.h" |
| 28 | +#include "../lib/simulator_custatevecex.h" |
| 29 | +#include "../lib/util_custatevec.h" |
| 30 | + |
| 31 | +struct Options { |
| 32 | + std::string circuit_file; |
| 33 | + unsigned maxtime = std::numeric_limits<unsigned>::max(); |
| 34 | + unsigned seed = 1; |
| 35 | + unsigned verbosity = 0; |
| 36 | +}; |
| 37 | + |
| 38 | +Options GetOptions(int argc, char* argv[]) { |
| 39 | + constexpr char usage[] = "usage:\n ./qsim_base -c circuit -d maxtime " |
| 40 | + "-s seed -v verbosity\n"; |
| 41 | + |
| 42 | + Options opt; |
| 43 | + |
| 44 | + int k; |
| 45 | + |
| 46 | + while ((k = getopt(argc, argv, "c:d:s:v:")) != -1) { |
| 47 | + switch (k) { |
| 48 | + case 'c': |
| 49 | + opt.circuit_file = optarg; |
| 50 | + break; |
| 51 | + case 'd': |
| 52 | + opt.maxtime = std::atoi(optarg); |
| 53 | + break; |
| 54 | + case 's': |
| 55 | + opt.seed = std::atoi(optarg); |
| 56 | + break; |
| 57 | + case 'v': |
| 58 | + opt.verbosity = std::atoi(optarg); |
| 59 | + break; |
| 60 | + default: |
| 61 | + qsim::IO::errorf(usage); |
| 62 | + exit(1); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return opt; |
| 67 | +} |
| 68 | + |
| 69 | +bool ValidateOptions(const Options& opt) { |
| 70 | + if (opt.circuit_file.empty()) { |
| 71 | + qsim::IO::errorf("circuit file is not provided.\n"); |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | +template <typename StateSpace, typename State> |
| 79 | +void PrintAmplitudes( |
| 80 | + unsigned num_qubits, const StateSpace& state_space, const State& state) { |
| 81 | + static constexpr char const* bits[8] = { |
| 82 | + "000", "001", "010", "011", "100", "101", "110", "111", |
| 83 | + }; |
| 84 | + |
| 85 | + uint64_t size = std::min(uint64_t{8}, uint64_t{1} << num_qubits); |
| 86 | + unsigned s = 3 - std::min(unsigned{3}, num_qubits); |
| 87 | + |
| 88 | + for (uint64_t i = 0; i < size; ++i) { |
| 89 | + auto a = state_space.GetAmpl(state, i); |
| 90 | + qsim::IO::messagef("%s:%16.8g%16.8g%16.8g\n", |
| 91 | + bits[i] + s, std::real(a), std::imag(a), std::norm(a)); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +int main(int argc, char* argv[]) { |
| 96 | + using namespace qsim; |
| 97 | + |
| 98 | + auto opt = GetOptions(argc, argv); |
| 99 | + if (!ValidateOptions(opt)) { |
| 100 | + return 1; |
| 101 | + } |
| 102 | + |
| 103 | + using fp_type = float; |
| 104 | + |
| 105 | + Circuit<GateQSim<fp_type>> circuit; |
| 106 | + if (!CircuitQsimParser<IOFile>::FromFile(opt.maxtime, opt.circuit_file, |
| 107 | + circuit)) { |
| 108 | + return 1; |
| 109 | + } |
| 110 | + |
| 111 | + struct Factory { |
| 112 | + using Simulator = qsim::SimulatorCuStateVecEx<fp_type>; |
| 113 | + using StateSpace = Simulator::StateSpace; |
| 114 | + |
| 115 | + explicit Factory(unsigned verbosity = 0) : verbosity(verbosity) { |
| 116 | + mp.initialize(); |
| 117 | + } |
| 118 | + |
| 119 | + StateSpace CreateStateSpace() const { |
| 120 | + StateSpace::Parameter param; |
| 121 | + param.verbosity = verbosity; |
| 122 | + |
| 123 | + return StateSpace{mp, param}; |
| 124 | + } |
| 125 | + |
| 126 | + Simulator CreateSimulator() const { |
| 127 | + return Simulator{}; |
| 128 | + } |
| 129 | + |
| 130 | + MultiProcessCuStateVecEx mp; |
| 131 | + unsigned verbosity; |
| 132 | + }; |
| 133 | + |
| 134 | + using Simulator = Factory::Simulator; |
| 135 | + using StateSpace = Simulator::StateSpace; |
| 136 | + using State = StateSpace::State; |
| 137 | + using Runner = CuStateVecExRunner<IO, Factory>; |
| 138 | + |
| 139 | + Factory factory(opt.verbosity); |
| 140 | + |
| 141 | + StateSpace state_space = factory.CreateStateSpace(); |
| 142 | + State state = state_space.Create(circuit.num_qubits); |
| 143 | + |
| 144 | + if (state_space.IsNull(state)) { |
| 145 | + IO::errorf("not enough memory: is the number of qubits too large?\n"); |
| 146 | + return 1; |
| 147 | + } |
| 148 | + |
| 149 | + state_space.SetStateZero(state); |
| 150 | + |
| 151 | + Runner::Parameter param; |
| 152 | + param.seed = opt.seed; |
| 153 | + param.verbosity = opt.verbosity; |
| 154 | + |
| 155 | + if (Runner::Run(param, factory, circuit, state)) { |
| 156 | + PrintAmplitudes(circuit.num_qubits, state_space, state); |
| 157 | + } |
| 158 | + |
| 159 | + return 0; |
| 160 | +} |
0 commit comments