-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82ca280
commit d7c95b7
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |