Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ endfunction()
add_application(circuit_test circuit_test.cpp)
add_application(observable_test observable_test.cpp)
add_application(target_test target_test.cpp)
add_application(parameterized_circuit_test parameterized_circuit_test.cpp)

if(QRMI_ROOT OR QISKIT_IBM_RUNTIME_C_ROOT OR SQC_ROOT)
add_application(sampler_test sampler_test.cpp)
Expand Down
53 changes: 53 additions & 0 deletions samples/parameterized_circuit_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
# This code is part of Qiskit.
#
# (C) Copyright IBM 2024. 2026.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.
*/

// Test parameterized circuit of Qiskit C++

#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdint>
#include <cstdlib>
#include <cmath>

#include "circuit/quantumcircuit.hpp"

using namespace Qiskit::circuit;

int main()
{
QuantumRegister qr(4);
ClassicalRegister cr(4);
QuantumCircuit circ(qr, cr);

Parameter theta("t");
Parameter a = theta + 0.5;

circ.h(0);
circ.rx(a, 1);

circ.measure(qr, cr);

circ.print();

circ.draw();


Parameter p1 = Parameter(M_PI/2.0);
Parameter p2 = Parameter(M_PI/2.0);
std::cout << (p1 == p2) << std::endl;


return 0;
}

4 changes: 2 additions & 2 deletions samples/target_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main()
}

std::cout << "input circuit" << std::endl;
circ.print();
circ.draw();


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

std::cout << "transpiled circuit" << std::endl;
transpiled.print();
transpiled.draw();

return 0;
}
Expand Down
6 changes: 3 additions & 3 deletions src/circuit/instruction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Instruction {
std::string name_;
uint_t num_qubits_;
uint_t num_clbits_;
std::vector<double> params_;
std::vector<Parameter> params_;
std::string label_;
QkGate map_ = QkGate_I;
bool is_standard_gate_ = false;
Expand Down Expand Up @@ -107,14 +107,14 @@ class Instruction {

/// @brief Return parameters of the instruction
/// @return parameters of the instruction
const std::vector<double>& params(void) const
const std::vector<Parameter>& params(void) const
{
return params_;
}

/// @brief set parameters to the instruction
/// @param parameters to be set
void set_params(const std::vector<double>& params)
void set_params(const std::vector<Parameter>& params)
{
params_ = params;
}
Expand Down
31 changes: 29 additions & 2 deletions src/circuit/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Parameter

/// @brief get string expression of the Parameter
/// @return a string expression of the Parameter
std::string as_str(void)
std::string as_str(void) const
{
char* str = qk_param_str(qiskit_param_.get());
std::string ret = str;
Expand Down Expand Up @@ -329,6 +329,25 @@ class Parameter
return !qk_param_equal(qiskit_param_.get(), rhs.qiskit_param_.get());
}


/// @brief compare the Parameter with a scalar
/// @param rhs a scalar to be compared
/// @return true if 2 are equal
bool operator==(const double rhs) const
{
Parameter r(rhs);
return *this == r;
}

/// @brief compare the Parameter with a scalar
/// @param rhs a scalar to be compared
/// @return true if 2 are not equal
bool operator!=(const double rhs) const
{
Parameter r(rhs);
return *this != r;
}

/// @brief calculate exponent of this Parameter
/// @return a new Parameter for the result
Parameter exp(void)
Expand Down Expand Up @@ -453,6 +472,9 @@ class Parameter
/// @return a new substituted Parameter
Parameter subs(const std::vector<Parameter>& symbols, const std::vector<Parameter>& others);
#endif

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

};

#ifdef QISKIT_CAPI_HAS_SUBS
Expand Down Expand Up @@ -502,9 +524,14 @@ Parameter Parameter::subs(const std::vector<Parameter>& symbol, const std::vecto
}
#endif

std::ostream& operator<<(std::ostream& os, const Parameter& p)
{
os << p.as_str();
return os;
}


} // namespace circuit
} // namespace circui
} // namespace Qiskit

#endif // __qiskitcpp_circuit_parameter_hpp__
2 changes: 2 additions & 0 deletions src/circuit/quantumcircuit_def.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,8 @@ class QuantumCircuit
/// @brief print circuit (this is for debug)
void print(void) const;

/// @brief draw the circuit
void draw(void) const;

/// @brief compare two circuits
/// @param other a circuit to be compared with this circuit
Expand Down
Loading