Skip to content

Commit

Permalink
Merge branch 'main' into set-space
Browse files Browse the repository at this point in the history
  • Loading branch information
gandalfr-KY committed Jan 24, 2025
2 parents ac2197c + d5aac87 commit 9420d56
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
12 changes: 10 additions & 2 deletions exe/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ def main():

print(operator.to_json())

states = StateVectorBatched(3, 3)
states = StateVectorBatched.Haar_random_state(2, 3)
prx = gate.ParamRX(0, 2.0, [1])
pry = gate.ParamRY(1, 2.0, [2])
params = {
"rx": [0.0, 0.1, 0.2],
"ry": [0.3, 0.4, 0.5]
}
circuit.add_param_gate(prx, "rx")
circuit.add_param_gate(pry, "ry")
circuit.update_quantum_state(states, params)
print(states.to_json())


if __name__ == "__main__":
main()
31 changes: 26 additions & 5 deletions include/scaluq/circuit/circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Circuit {

void update_quantum_state(StateVector<Fp, Sp>& state,
const std::map<std::string, Fp>& parameters = {}) const;
void update_quantum_state(StateVectorBatched<Fp, Sp>& states,
const std::map<std::string, std::vector<Fp>>& parameters = {}) const;

Circuit copy() const;

Expand Down Expand Up @@ -128,7 +130,8 @@ void bind_circuit_circuit_hpp(nb::module_& m) {
nb::overload_cast<const Circuit<Fp, Sp>&>(&Circuit<Fp, Sp>::add_circuit),
"Add all gates in specified circuit. Given gates are copied.")
.def("update_quantum_state",
&Circuit<Fp, Sp>::update_quantum_state,
nb::overload_cast<StateVector<Fp, Sp>&, const std::map<std::string, Fp>&>(
&Circuit<Fp, Sp>::update_quantum_state, nb::const_),
"Apply gate to the StateVector. StateVector in args is directly updated. If the "
"circuit contains parametric gate, you have to give real value of parameter as "
"dict[str, float] in 2nd arg.")
Expand All @@ -144,10 +147,28 @@ void bind_circuit_circuit_hpp(nb::module_& m) {
"Apply gate to the StateVector. StateVector in args is directly updated. If the "
"circuit contains parametric gate, you have to give real value of parameter as "
"\"name=value\" format in kwargs.")
.def("update_quantum_state",
[](const Circuit<Fp>& circuit, StateVector<Fp, Sp>& state) {
circuit.update_quantum_state(state);
})
.def(
"update_quantum_state",
nb::overload_cast<StateVectorBatched<Fp, Sp>&,
const std::map<std::string, std::vector<Fp, Sp>>&>(
&Circuit<Fp, Sp>::update_quantum_state, nb::const_),
"Apply gate to the StateVectorBatched. StateVectorBatched in args is directly updated. "
"If the circuit contains parametric gate, you have to give real value of parameter as "
"dict[str, list[float]] in 2nd arg.")
.def(
"update_quantum_state",
[&](const Circuit<Fp, Sp>& circuit,
StateVectorBatched<Fp, Sp>& states,
nb::kwargs kwargs) {
std::map<std::string, std::vector<Fp>> parameters;
for (auto&& [key, param] : kwargs) {
parameters[nb::cast<std::string>(key)] = nb::cast<std::vector<Fp>>(param);
}
circuit.update_quantum_state(states, parameters);
},
"Apply gate to the StateVectorBatched. StateVectorBatched in args is directly updated. "
"If the circuit contains parametric gate, you have to give real value of parameter as "
"\"name=[value1, value2, ...]\" format in kwargs.")
.def("copy", &Circuit<Fp, Sp>::copy, "Copy circuit. All the gates inside is copied.")
.def("get_inverse",
&Circuit<Fp, Sp>::get_inverse,
Expand Down
24 changes: 24 additions & 0 deletions src/circuit/circuit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,30 @@ void Circuit<Fp, Sp>::update_quantum_state(StateVector<Fp, Sp>& state,
}
}

FLOAT_AND_SPACE(Fp, Sp)
void Circuit<Fp, Sp>::update_quantum_state(
StateVectorBatched<Fp, Sp>& states,
const std::map<std::string, std::vector<Fp>>& parameters) const {
for (auto&& gate : _gate_list) {
if (gate.index() == 0) continue;
const auto& key = std::get<1>(gate).second;
if (!parameters.contains(key)) {
using namespace std::string_literals;
throw std::runtime_error(
"Circuit::update_quantum_state(StateVector&, const std::map<std::string_view, double>&) const: parameter named "s +
std::string(key) + "is not given.");
}
}
for (auto&& gate : _gate_list) {
if (gate.index() == 0) {
std::get<0>(gate)->update_quantum_state(states);
} else {
const auto& [param_gate, key] = std::get<1>(gate);
param_gate->update_quantum_state(states, parameters.at(key));
}
}
}

FLOAT_AND_SPACE(Fp, Sp)
Circuit<Fp, Sp> Circuit<Fp, Sp>::copy() const {
Circuit ccircuit(_n_qubits);
Expand Down

0 comments on commit 9420d56

Please sign in to comment.