Skip to content

Commit 4758562

Browse files
authored
Updates for Qiskit 2.4, add parameterized gates (#142)
* Updates for Qiskit 2.4, add parameterized gates * fix test cases
1 parent 755db90 commit 4758562

9 files changed

Lines changed: 330 additions & 248 deletions

File tree

samples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ endfunction()
102102
add_application(circuit_test circuit_test.cpp)
103103
add_application(observable_test observable_test.cpp)
104104
add_application(target_test target_test.cpp)
105+
add_application(parameterized_circuit_test parameterized_circuit_test.cpp)
105106

106107
if(QRMI_ROOT OR QISKIT_IBM_RUNTIME_C_ROOT OR SQC_ROOT)
107108
add_application(sampler_test sampler_test.cpp)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
# This code is part of Qiskit.
3+
#
4+
# (C) Copyright IBM 2024. 2026.
5+
#
6+
# This code is licensed under the Apache License, Version 2.0. You may
7+
# obtain a copy of this license in the LICENSE.txt file in the root directory
8+
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
9+
#
10+
# Any modifications or derivative works of this code must retain this
11+
# copyright notice, and modified files need to carry a notice indicating
12+
# that they have been altered from the originals.
13+
*/
14+
15+
// Test parameterized circuit of Qiskit C++
16+
17+
#define _USE_MATH_DEFINES
18+
#include <iostream>
19+
#include <cstdint>
20+
#include <cstdlib>
21+
#include <cmath>
22+
23+
#include "circuit/quantumcircuit.hpp"
24+
25+
using namespace Qiskit::circuit;
26+
27+
int main()
28+
{
29+
QuantumRegister qr(4);
30+
ClassicalRegister cr(4);
31+
QuantumCircuit circ(qr, cr);
32+
33+
Parameter theta("t");
34+
Parameter a = theta + 0.5;
35+
36+
circ.h(0);
37+
circ.rx(a, 1);
38+
39+
circ.measure(qr, cr);
40+
41+
circ.print();
42+
43+
circ.draw();
44+
45+
46+
Parameter p1 = Parameter(M_PI/2.0);
47+
Parameter p2 = Parameter(M_PI/2.0);
48+
std::cout << (p1 == p2) << std::endl;
49+
50+
51+
return 0;
52+
}
53+

samples/target_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main()
4343
}
4444

4545
std::cout << "input circuit" << std::endl;
46-
circ.print();
46+
circ.draw();
4747

4848

4949
auto target = Target({"h", "cx"}, {{0, 1}, {1, 0}, {1, 3}, {3, 1}, {0, 2}, {2, 0}, {2, 3}, {3, 2}});
@@ -53,7 +53,7 @@ int main()
5353
auto transpiled = pass.run(circ);
5454

5555
std::cout << "transpiled circuit" << std::endl;
56-
transpiled.print();
56+
transpiled.draw();
5757

5858
return 0;
5959
}

src/circuit/instruction.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Instruction {
3838
std::string name_;
3939
uint_t num_qubits_;
4040
uint_t num_clbits_;
41-
std::vector<double> params_;
41+
std::vector<Parameter> params_;
4242
std::string label_;
4343
QkGate map_ = QkGate_I;
4444
bool is_standard_gate_ = false;
@@ -107,14 +107,14 @@ class Instruction {
107107

108108
/// @brief Return parameters of the instruction
109109
/// @return parameters of the instruction
110-
const std::vector<double>& params(void) const
110+
const std::vector<Parameter>& params(void) const
111111
{
112112
return params_;
113113
}
114114

115115
/// @brief set parameters to the instruction
116116
/// @param parameters to be set
117-
void set_params(const std::vector<double>& params)
117+
void set_params(const std::vector<Parameter>& params)
118118
{
119119
params_ = params;
120120
}

src/circuit/parameter.hpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class Parameter
9393

9494
/// @brief get string expression of the Parameter
9595
/// @return a string expression of the Parameter
96-
std::string as_str(void)
96+
std::string as_str(void) const
9797
{
9898
char* str = qk_param_str(qiskit_param_.get());
9999
std::string ret = str;
@@ -329,6 +329,25 @@ class Parameter
329329
return !qk_param_equal(qiskit_param_.get(), rhs.qiskit_param_.get());
330330
}
331331

332+
333+
/// @brief compare the Parameter with a scalar
334+
/// @param rhs a scalar to be compared
335+
/// @return true if 2 are equal
336+
bool operator==(const double rhs) const
337+
{
338+
Parameter r(rhs);
339+
return *this == r;
340+
}
341+
342+
/// @brief compare the Parameter with a scalar
343+
/// @param rhs a scalar to be compared
344+
/// @return true if 2 are not equal
345+
bool operator!=(const double rhs) const
346+
{
347+
Parameter r(rhs);
348+
return *this != r;
349+
}
350+
332351
/// @brief calculate exponent of this Parameter
333352
/// @return a new Parameter for the result
334353
Parameter exp(void)
@@ -453,6 +472,9 @@ class Parameter
453472
/// @return a new substituted Parameter
454473
Parameter subs(const std::vector<Parameter>& symbols, const std::vector<Parameter>& others);
455474
#endif
475+
476+
friend std::ostream& operator<<(std::ostream& os, const Parameter& p);
477+
456478
};
457479

458480
#ifdef QISKIT_CAPI_HAS_SUBS
@@ -502,9 +524,14 @@ Parameter Parameter::subs(const std::vector<Parameter>& symbol, const std::vecto
502524
}
503525
#endif
504526

527+
std::ostream& operator<<(std::ostream& os, const Parameter& p)
528+
{
529+
os << p.as_str();
530+
return os;
531+
}
505532

506533

507-
} // namespace circuit
534+
} // namespace circui
508535
} // namespace Qiskit
509536

510537
#endif // __qiskitcpp_circuit_parameter_hpp__

src/circuit/quantumcircuit_def.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,8 @@ class QuantumCircuit
689689
/// @brief print circuit (this is for debug)
690690
void print(void) const;
691691

692+
/// @brief draw the circuit
693+
void draw(void) const;
692694

693695
/// @brief compare two circuits
694696
/// @param other a circuit to be compared with this circuit

0 commit comments

Comments
 (0)