Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactoring] Take measurement register out of QuantumState #162

Open
rturrado opened this issue Aug 16, 2024 · 0 comments
Open

[Refactoring] Take measurement register out of QuantumState #162

rturrado opened this issue Aug 16, 2024 · 0 comments

Comments

@rturrado
Copy link
Contributor

A QuantumState holds a quantum state (SparseArray data) and a measurement register (BasisVector measurementRegister).
I think this is wrong because a quantum state and a measurement register are different things. A quantum state should just contain a representation of the state, for example, as it does at the moment, via a data member of type SparseArray.

When we call Circuit::execute, we pass a QuantumState. But we already have a SimulationResultAccumulator around. And a SimulationResultAccumulator also holds a quantum state (QuantumState state) and a measurement register (absl::btree_map<state_string_t, count_t> measurements plus std::uint64_t measurementsCount.

These lines:

std::variant<std::monostate, SimulationResult, SimulationError> execute
        auto simulationResultAccumulator = SimulationResultAccumulator{ quantumState };

        while (iterations--) {
            quantumState.reset();
            circuit.execute(quantumState, std::monostate{});
            simulationResultAccumulator.appendMeasurement(quantumState.getMeasurementRegister());
        }

void Circuit::execute(core::QuantumState &quantumState, error_models::ErrorModel const &errorModel) const {
    InstructionExecutor instruction_executor(quantumState);

could be turned into:

std::variant<std::monostate, SimulationResult, SimulationError> execute
        auto simulationResultAccumulator = SimulationResultAccumulator{};

        while (iterations--) {
            auto measurementRegister = core::BasisVector{};
            circuit.execute(QuantumState{}, measurementRegister, std::monostate{});
            simulationResultAccumulator.appendMeasurement(measurementRegister);
        }

void Circuit::execute(core::QuantumState &quantumState, core::BasisVector &measurementRegister, error_models::ErrorModel const &errorModel) const {
    InstructionExecutor instruction_executor(quantumState, measurementRegister);

Notice that the quantum state is reset for every simulation run. The SimulationResultAccumulator just uses the quantum state from the last execution.

We could probably go further and group a quantum state and a measurement register into a SimulationIterationResult. Then we would write the code above as:

std::variant<std::monostate, SimulationResult, SimulationError> execute
        auto simulationResultAccumulator = SimulationResultAccumulator{};

        while (iterations--) {
            simulationIterationResult = circuit.execute(std::monostate{});
            simulationResultAccumulator.appendMeasurement(simulationIterationResult.getMeasurementRegister());
        }

core::SimulationResult &Circuit::execute(error_models::ErrorModel const &errorModel) const {
    InstructionExecutor instruction_executor{};
    if (auto *measure = std::get_if<Measure>(&instruction)) {
            return instruction_executor(*measure);  // every instruction returns a SimulationIterationResult

Even rewrite the accumulation as:

    auto simulationResultAccumulator = ranges::accumulate(
        ranges::iota(0, iterations-1),
        SimulationResultAccumulator{}, [&circuit](auto &acc, auto _) {
            simulationIterationResult = circuit.execute(std::monostate{});
            acc.appendMeasurement(simulationIterationResult);
            return acc;
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant