Skip to content

Commit 91f4b5d

Browse files
authored
Restructure codes to remove multiple definitions (#144)
* Restore test to separate test codes * remove all multi definition errors * fix for Windows test * fix for Windows test * fix for Windows test
1 parent fe0e666 commit 91f4b5d

27 files changed

Lines changed: 2376 additions & 2741 deletions

samples/circuit_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int main()
7070
circ.barrier(0);
7171
circ.measure(qr, cr);
7272

73-
circ.print();
73+
circ.draw();
7474
std::cout << circ.to_qasm3();
7575

7676
return 0;

src/circuit/classicalregister.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
#ifndef __qiskitcpp_circuit_classical_register_hpp__
1818
#define __qiskitcpp_circuit_classical_register_hpp__
1919

20+
#include <atomic>
21+
2022
#include "circuit/register.hpp"
2123
#include "qiskit.h"
2224

25+
26+
namespace {
27+
std::atomic<uint64_t> CR_counter(0ull);
28+
}
29+
2330
namespace Qiskit
2431
{
2532
namespace circuit
@@ -38,7 +45,7 @@ class ClassicalRegister : public Register
3845
{
3946
protected:
4047
std::shared_ptr<QkClassicalRegister> rust_register_ = nullptr;
41-
static uint_t instances_counter_;
48+
// static uint_t instances_counter_;
4249

4350
public:
4451
/// @brief Create a new ClassicalRegister
@@ -78,7 +85,7 @@ class ClassicalRegister : public Register
7885
std::string prefix(void) override
7986
{
8087
std::string ret = "c";
81-
ret += std::to_string(instances_counter_++);
88+
ret += std::to_string(CR_counter++);
8289
return ret;
8390
}
8491

@@ -120,7 +127,7 @@ class ClassicalRegister : public Register
120127
Binary operator>=(Expr &right);
121128
};
122129

123-
uint_t ClassicalRegister::instances_counter_ = 0;
130+
//inline uint_t ClassicalRegister::instances_counter_ = 0;
124131

125132
} // namespace circuit
126133
} // namespace Qiskit

src/circuit/controlflow/control_flow.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#ifndef __qiskitcpp_circuit_control_flow_hpp__
1818
#define __qiskitcpp_circuit_control_flow_hpp__
1919

20-
#include "circuit/quantumcircuit_def.hpp"
2120

2221
namespace Qiskit {
2322
namespace circuit {

src/circuit/controlflow/if_else.hpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,36 +49,31 @@ class IfElseOp : public ControlFlowOp
4949

5050
// add false body
5151
/// @brief add false body
52-
void else_(std::function<void(QuantumCircuit&)> body);
53-
54-
void add_control_flow_op(QuantumCircuit& circ) override;
55-
};
56-
57-
void IfElseOp::else_(std::function<void(QuantumCircuit&)> body)
58-
{
59-
body(false_body_);
60-
test_else_ = true;
61-
}
62-
63-
void IfElseOp::add_control_flow_op(QuantumCircuit& circ)
64-
{
65-
if(test_else_){
66-
// if_else
67-
std::shared_ptr<rust_circuit> true_circ = true_body_.get_rust_circuit();
68-
std::shared_ptr<rust_circuit> false_circ = false_body_.get_rust_circuit();
69-
70-
// TO DO : Add if_else support in Qiskit C-API (maybe in the future?)
71-
// qc_if_else(circ.get_rust_circuit(false), clbit_, value_, true_circ, false_circ);
52+
void else_(std::function<void(QuantumCircuit&)> body)
53+
{
54+
body(false_body_);
55+
test_else_ = true;
7256
}
73-
else{
74-
// if_test
75-
std::shared_ptr<rust_circuit> true_circ = true_body_.get_rust_circuit();
7657

77-
// TO DO : Add if_else support in Qiskit C-API (maybe in the future?)
78-
//qc_if_test(circ.get_rust_circuit(false), clbit_, value_, true_circ);
58+
void add_control_flow_op(QuantumCircuit& circ) override
59+
{
60+
if(test_else_){
61+
// if_else
62+
std::shared_ptr<rust_circuit> true_circ = true_body_.get_rust_circuit();
63+
std::shared_ptr<rust_circuit> false_circ = false_body_.get_rust_circuit();
64+
65+
// TO DO : Add if_else support in Qiskit C-API (maybe in the future?)
66+
// qc_if_else(circ.get_rust_circuit(false), clbit_, value_, true_circ, false_circ);
67+
}
68+
else{
69+
// if_test
70+
std::shared_ptr<rust_circuit> true_circ = true_body_.get_rust_circuit();
71+
72+
// TO DO : Add if_else support in Qiskit C-API (maybe in the future?)
73+
//qc_if_test(circ.get_rust_circuit(false), clbit_, value_, true_circ);
74+
}
7975
}
80-
}
81-
76+
};
8277

8378

8479
} // namespace circuit

src/circuit/library/standard_gates/standard_gates.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
namespace Qiskit {
5252
namespace circuit {
5353

54-
std::unordered_map<std::string, Instruction> get_standard_gate_name_mapping(void)
54+
inline std::unordered_map<std::string, Instruction> get_standard_gate_name_mapping(void)
5555
{
5656
// mapping of gate string and QkGate
5757
std::unordered_map<std::string, Instruction> name_mapping;

src/circuit/parameter.hpp

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -452,79 +452,69 @@ class Parameter
452452
/// @param symbol a symbol to be bound
453453
/// @param value a value to be set
454454
/// @return a new bound Parameter
455-
Parameter bind(const Parameter& symbol, const double value);
455+
Parameter bind(const Parameter& symbol, const double value)
456+
{
457+
Parameter ret;
458+
459+
qk_param_bind(ret.qiskit_param_.get(), qiskit_param_.get(), &symbol.qiskit_param_.get(), &value, 1);
460+
return ret;
461+
}
456462

457463
/// @brief bind values to symbols
458464
/// @param symbols a list symbols to be bound
459465
/// @param value a list of values to be set
460466
/// @return a new bound Parameter
461-
Parameter bind(const std::vector<Parameter>& symbols, const std::vector<double> values);
467+
Parameter bind(const std::vector<Parameter>& symbols, const std::vector<double> values)
468+
{
469+
size_t size = std::min(symbols.size(), values.size());
470+
Parameter ret;
471+
std::vector<qiskit_param*> list(size);
472+
for (uint_t i = 0; i < size; i++) {
473+
list[i] = symbols[i].qiskit_param_.get();
474+
}
475+
476+
qk_param_bind(ret.qiskit_param_.get(), qiskit_param_.get(), list.data(), values.data(), size);
477+
return ret;
478+
479+
}
462480

463481
/// @brief substitute a symbol to other symbol
464482
/// @param symbol a symbol to be bound
465483
/// @param other a symbol to be set
466484
/// @return a new substituted Parameter
467-
Parameter subs(const Parameter& symbol, const Parameter& other);
485+
Parameter subs(const Parameter& symbol, const Parameter& other)
486+
{
487+
Parameter ret;
488+
489+
qk_param_subs(ret.qiskit_param_.get(), qiskit_param_.get(), &symbol.qiskit_param_.get(), &other.qiskit_param_.get(), 1);
490+
return ret;
491+
}
468492

469493
/// @brief substitute symbols to other symbols
470494
/// @param symbols a list of symbols to be bound
471495
/// @param others a list of symbols to be set
472496
/// @return a new substituted Parameter
473-
Parameter subs(const std::vector<Parameter>& symbols, const std::vector<Parameter>& others);
497+
Parameter subs(const std::vector<Parameter>& symbols, const std::vector<Parameter>& others)
498+
{
499+
size_t size = std::min(symbols.size(), values.size());
500+
Parameter ret;
501+
std::vector<qiskit_param*> slist(size);
502+
std::vector<qiskit_param*> olist(size);
503+
for (uint_t i = 0; i < size; i++) {
504+
slist[i] = symbols[i].qiskit_param_.get();
505+
olist[i] = others[i].qiskit_param_.get();
506+
}
507+
508+
qk_param_bind(ret.qiskit_param_.get(), qiskit_param_.get(), slist.data(), olist.data(), size);
509+
return ret;
510+
}
474511
#endif
475512

476513
friend std::ostream& operator<<(std::ostream& os, const Parameter& p);
477514

478515
};
479516

480-
#ifdef QISKIT_CAPI_HAS_SUBS
481-
Parameter Parameter::bind(const Parameter& symbol, const double value)
482-
{
483-
Parameter ret;
484-
485-
qk_param_bind(ret.qiskit_param_.get(), qiskit_param_.get(), &symbol.qiskit_param_.get(), &value, 1);
486-
return ret;
487-
}
488-
489-
Parameter Parameter::bind(const std::vector<Parameter>& symbols, const std::vector<double> values)
490-
{
491-
size_t size = std::min(symbols.size(), values.size());
492-
Parameter ret;
493-
std::vector<qiskit_param*> list(size);
494-
for (uint_t i = 0; i < size; i++) {
495-
list[i] = symbols[i].qiskit_param_.get();
496-
}
497-
498-
qk_param_bind(ret.qiskit_param_.get(), qiskit_param_.get(), list.data(), values.data(), size);
499-
return ret;
500-
501-
}
502-
503-
Parameter Parameter::subs(const Parameter& symbol, const Parameter& other)
504-
{
505-
Parameter ret;
506-
507-
qk_param_subs(ret.qiskit_param_.get(), qiskit_param_.get(), &symbol.qiskit_param_.get(), &other.qiskit_param_.get(), 1);
508-
return ret;
509-
}
510-
511-
Parameter Parameter::subs(const std::vector<Parameter>& symbol, const std::vector<Parameter>& others)
512-
{
513-
size_t size = std::min(symbols.size(), values.size());
514-
Parameter ret;
515-
std::vector<qiskit_param*> slist(size);
516-
std::vector<qiskit_param*> olist(size);
517-
for (uint_t i = 0; i < size; i++) {
518-
slist[i] = symbols[i].qiskit_param_.get();
519-
olist[i] = others[i].qiskit_param_.get();
520-
}
521-
522-
qk_param_bind(ret.qiskit_param_.get(), qiskit_param_.get(), slist.data(), olist.data(), size);
523-
return ret;
524-
}
525-
#endif
526-
527-
std::ostream& operator<<(std::ostream& os, const Parameter& p)
517+
inline std::ostream& operator<<(std::ostream& os, const Parameter& p)
528518
{
529519
os << p.as_str();
530520
return os;

src/circuit/quantumcircuit.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@
2222
#include "circuit/quantumcircuit_impl.hpp"
2323

2424
#endif // __qiskitcpp_circuit_quantum_circuit_hpp__
25-

0 commit comments

Comments
 (0)