Skip to content

Commit

Permalink
implement circuit base
Browse files Browse the repository at this point in the history
  • Loading branch information
KowerKoint committed Jan 30, 2024
1 parent 82ca280 commit d7c95b7
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions qulacs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.21)

target_sources(qulacs PRIVATE
circuit/circuit.cpp
gate/gate_npair_qubit.cpp
gate/gate_one_control_one_target.cpp
gate/gate_one_qubit.cpp
Expand Down
3 changes: 3 additions & 0 deletions qulacs/all.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#pragma once

#include "circuit/circuit.hpp"
#include "gate/constant.hpp"
#include "gate/gate.hpp"
#include "gate/gate_npair_qubit.hpp"
Expand Down
43 changes: 43 additions & 0 deletions qulacs/circuit/circuit.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "circuit.hpp"

#include <ranges>

namespace qulacs {
void Circuit::add_gate(const Gate& gate) {
check_gate_is_valid(gate);
_gate_list.push_back(gate);
}

void Circuit::update_quantum_state(StateVector& state) const {
for (const auto& gate : _gate_list) {
gate->update_quantum_state(state);
}
}

Circuit Circuit::copy() const {
Circuit ccircuit(_n_qubits);
for (const auto& gate : _gate_list) {
ccircuit.add_gate(gate->copy());
}
return ccircuit;
}

Circuit Circuit::get_inverse() const {
Circuit icircuit(_n_qubits);
for (const auto& gate : _gate_list | std::views::reverse) {
icircuit.add_gate(gate->get_inverse());
}
return icircuit;
}

void Circuit::check_gate_is_valid(const Gate& gate) const {
auto targets = gate->get_target_qubit_list();
auto controls = gate->get_control_qubit_list();
bool valid = true;
if (!targets.empty()) valid &= *std::max_element(targets.begin(), targets.end()) < _n_qubits;
if (!controls.empty()) valid &= *std::max_element(controls.begin(), controls.end()) < _n_qubits;
if (!valid) {
throw std::runtime_error("Gate to be added to the circuit has invalid qubit range");
}
}
} // namespace qulacs
40 changes: 40 additions & 0 deletions qulacs/circuit/circuit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "../gate/gate.hpp"
#include "../types.hpp"

namespace qulacs {
class Circuit {
public:
explicit Circuit(UINT n_qubits) : _n_qubits(n_qubits) {}

[[nodiscard]] inline UINT n_qubits() const { return _n_qubits; }
[[nodiscard]] inline const std::vector<Gate>& gate_list() const { return _gate_list; }
[[nodiscard]] inline const Gate& get(UINT idx) const {
if (idx >= _gate_list.size()) {
throw std::runtime_error("Circuit::get(UINT): index out of bounds");
}
return _gate_list[idx];
}
[[nodiscard]] inline Gate& get(UINT idx) {
if (idx >= _gate_list.size()) {
throw std::runtime_error("Circuit::get(UINT): index out of bounds");
}
return _gate_list[idx];
}

void add_gate(const Gate& gate);

void update_quantum_state(StateVector& state) const;

Circuit copy() const;
Circuit get_inverse() const;

private:
UINT _n_qubits;

std::vector<Gate> _gate_list;

void check_gate_is_valid(const Gate& gate) const;
};
} // namespace qulacs

0 comments on commit d7c95b7

Please sign in to comment.