mindquantum doc format
This commit is contained in:
parent
0ac3cd3aef
commit
576deb9a0c
|
@ -75,9 +75,12 @@ void ComputerForwardBackward(const std::shared_ptr<ComputeParam> &input_params,
|
|||
MS_EXCEPTION_IF_NULL(encoder_params_names);
|
||||
MS_EXCEPTION_IF_NULL(ansatz_params_names);
|
||||
MS_EXCEPTION_IF_NULL(tmp_sims);
|
||||
|
||||
if (end * hams->size() > result_len || end * encoder_params_names->size() * hams->size() > encoder_g_len ||
|
||||
end * ansatz_params_names->size() * hams->size() > ansatz_g_len) {
|
||||
auto mea_size = hams->size();
|
||||
if (is_projector) {
|
||||
mea_size = projectors->size();
|
||||
}
|
||||
if (end * mea_size > result_len || end * encoder_params_names->size() * mea_size > encoder_g_len ||
|
||||
end * ansatz_params_names->size() * mea_size > ansatz_g_len) {
|
||||
MS_LOG(EXCEPTION) << "pqc error input size!";
|
||||
}
|
||||
mindquantum::ParameterResolver pr;
|
||||
|
@ -107,18 +110,18 @@ void ComputerForwardBackward(const std::shared_ptr<ComputeParam> &input_params,
|
|||
auto energy = e0_grad1_grad_2[0];
|
||||
auto grad_encoder = e0_grad1_grad_2[1];
|
||||
auto grad_ansatz = e0_grad1_grad_2[2];
|
||||
if (energy.size() != hams->size() || grad_encoder.size() != encoder_params_names->size() * hams->size() ||
|
||||
grad_ansatz.size() != ansatz_params_names->size() * hams->size()) {
|
||||
if (energy.size() != mea_size || grad_encoder.size() != encoder_params_names->size() * mea_size ||
|
||||
grad_ansatz.size() != ansatz_params_names->size() * mea_size) {
|
||||
MS_LOG(EXCEPTION) << "pqc error evolution or batch size!";
|
||||
}
|
||||
for (size_t poi = 0; poi < hams->size(); poi++) {
|
||||
output[n * hams->size() + poi] = energy[poi];
|
||||
for (size_t poi = 0; poi < mea_size; poi++) {
|
||||
output[n * mea_size + poi] = energy[poi];
|
||||
}
|
||||
for (size_t poi = 0; poi < encoder_params_names->size() * hams->size(); poi++) {
|
||||
gradient_encoder[n * hams->size() * encoder_params_names->size() + poi] = grad_encoder[poi];
|
||||
for (size_t poi = 0; poi < encoder_params_names->size() * mea_size; poi++) {
|
||||
gradient_encoder[n * mea_size * encoder_params_names->size() + poi] = grad_encoder[poi];
|
||||
}
|
||||
for (size_t poi = 0; poi < ansatz_params_names->size() * hams->size(); poi++) {
|
||||
gradient_ansatz[n * hams->size() * ansatz_params_names->size() + poi] = grad_ansatz[poi];
|
||||
for (size_t poi = 0; poi < ansatz_params_names->size() * mea_size; poi++) {
|
||||
gradient_ansatz[n * mea_size * ansatz_params_names->size() + poi] = grad_ansatz[poi];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ class BasicCircuit {
|
|||
void AppendNoneParameterGate(const std::string &, Matrix, Indexes, Indexes);
|
||||
void AppendParameterGate(const std::string &, Indexes, Indexes, const ParameterResolver &);
|
||||
const GateBlocks &GetGateBlocks() const;
|
||||
~BasicCircuit() {}
|
||||
};
|
||||
} // namespace mindquantum
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -38,6 +38,8 @@ Matrix RXGate::GetIntrinsicDiffMatrix(CalcType theta) {
|
|||
RYGate::RYGate(const Indexes &obj_qubits, const Indexes &ctrl_qubits, const ParameterResolver ¶s)
|
||||
: IntrinsicOneParaGate("RY", obj_qubits, ctrl_qubits, paras) {}
|
||||
|
||||
RYGate::RYGate() : IntrinsicOneParaGate("RY", {}, {}, {}) {}
|
||||
|
||||
Matrix RYGate::GetIntrinsicMatrix(CalcType theta) {
|
||||
Matrix result = {{{cos(theta / 2), 0}, {-sin(theta / 2), 0}}, {{sin(theta / 2), 0}, {cos(theta / 2), 0}}};
|
||||
return result;
|
||||
|
@ -52,6 +54,8 @@ Matrix RYGate::GetIntrinsicDiffMatrix(CalcType theta) {
|
|||
RZGate::RZGate(const Indexes &obj_qubits, const Indexes &ctrl_qubits, const ParameterResolver ¶s)
|
||||
: IntrinsicOneParaGate("RZ", obj_qubits, ctrl_qubits, paras) {}
|
||||
|
||||
RZGate::RZGate() : IntrinsicOneParaGate("RZ", {}, {}, {}) {}
|
||||
|
||||
Matrix RZGate::GetIntrinsicMatrix(CalcType theta) {
|
||||
Matrix result = {{{cos(theta / 2), -sin(theta / 2)}, {0, 0}}, {{0, 0}, {cos(theta / 2), sin(theta / 2)}}};
|
||||
return result;
|
||||
|
@ -66,6 +70,8 @@ Matrix RZGate::GetIntrinsicDiffMatrix(CalcType theta) {
|
|||
PhaseShiftGate::PhaseShiftGate(const Indexes &obj_qubits, const Indexes &ctrl_qubits, const ParameterResolver ¶s)
|
||||
: IntrinsicOneParaGate("PS", obj_qubits, ctrl_qubits, paras) {}
|
||||
|
||||
PhaseShiftGate::PhaseShiftGate() : IntrinsicOneParaGate("PS", {}, {}, {}) {}
|
||||
|
||||
Matrix PhaseShiftGate::GetIntrinsicMatrix(CalcType theta) {
|
||||
Matrix result = {{{1, 0}, {0, 0}}, {{0, 0}, {cos(theta), sin(theta)}}};
|
||||
return result;
|
||||
|
@ -79,6 +85,8 @@ Matrix PhaseShiftGate::GetIntrinsicDiffMatrix(CalcType theta) {
|
|||
XXGate::XXGate(const Indexes &obj_qubits, const Indexes &ctrl_qubits, const ParameterResolver ¶s)
|
||||
: IntrinsicOneParaGate("XX", obj_qubits, ctrl_qubits, paras) {}
|
||||
|
||||
XXGate::XXGate() : IntrinsicOneParaGate("XX", {}, {}, {}) {}
|
||||
|
||||
Matrix XXGate::GetIntrinsicMatrix(CalcType theta) {
|
||||
double c = cos(theta);
|
||||
double s = sin(theta);
|
||||
|
@ -104,6 +112,8 @@ Matrix XXGate::GetIntrinsicDiffMatrix(CalcType theta) {
|
|||
YYGate::YYGate(const Indexes &obj_qubits, const Indexes &ctrl_qubits, const ParameterResolver ¶s)
|
||||
: IntrinsicOneParaGate("YY", obj_qubits, ctrl_qubits, paras) {}
|
||||
|
||||
YYGate::YYGate() : IntrinsicOneParaGate("YY", {}, {}, {}) {}
|
||||
|
||||
Matrix YYGate::GetIntrinsicMatrix(CalcType theta) {
|
||||
double c = cos(theta);
|
||||
double s = sin(theta);
|
||||
|
@ -129,6 +139,8 @@ Matrix YYGate::GetIntrinsicDiffMatrix(CalcType theta) {
|
|||
ZZGate::ZZGate(const Indexes &obj_qubits, const Indexes &ctrl_qubits, const ParameterResolver ¶s)
|
||||
: IntrinsicOneParaGate("ZZ", obj_qubits, ctrl_qubits, paras) {}
|
||||
|
||||
ZZGate::ZZGate() : IntrinsicOneParaGate("ZZ", {}, {}, {}) {}
|
||||
|
||||
Matrix ZZGate::GetIntrinsicMatrix(CalcType theta) {
|
||||
double c = cos(theta);
|
||||
double s = sin(theta);
|
||||
|
|
|
@ -28,6 +28,7 @@ class RXGate : public IntrinsicOneParaGate {
|
|||
public:
|
||||
RXGate(const Indexes &, const Indexes &, const ParameterResolver &);
|
||||
RXGate();
|
||||
~RXGate() {}
|
||||
};
|
||||
|
||||
class RYGate : public IntrinsicOneParaGate {
|
||||
|
@ -36,6 +37,8 @@ class RYGate : public IntrinsicOneParaGate {
|
|||
|
||||
public:
|
||||
RYGate(const Indexes &, const Indexes &, const ParameterResolver &);
|
||||
RYGate();
|
||||
~RYGate() {}
|
||||
};
|
||||
|
||||
class RZGate : public IntrinsicOneParaGate {
|
||||
|
@ -44,6 +47,8 @@ class RZGate : public IntrinsicOneParaGate {
|
|||
|
||||
public:
|
||||
RZGate(const Indexes &, const Indexes &, const ParameterResolver &);
|
||||
RZGate();
|
||||
~RZGate() {}
|
||||
};
|
||||
|
||||
class PhaseShiftGate : public IntrinsicOneParaGate {
|
||||
|
@ -52,6 +57,8 @@ class PhaseShiftGate : public IntrinsicOneParaGate {
|
|||
|
||||
public:
|
||||
PhaseShiftGate(const Indexes &, const Indexes &, const ParameterResolver &);
|
||||
PhaseShiftGate();
|
||||
~PhaseShiftGate() {}
|
||||
};
|
||||
|
||||
class XXGate : public IntrinsicOneParaGate {
|
||||
|
@ -60,6 +67,8 @@ class XXGate : public IntrinsicOneParaGate {
|
|||
|
||||
public:
|
||||
XXGate(const Indexes &, const Indexes &, const ParameterResolver &);
|
||||
XXGate();
|
||||
~XXGate() {}
|
||||
};
|
||||
|
||||
class YYGate : public IntrinsicOneParaGate {
|
||||
|
@ -68,6 +77,8 @@ class YYGate : public IntrinsicOneParaGate {
|
|||
|
||||
public:
|
||||
YYGate(const Indexes &, const Indexes &, const ParameterResolver &);
|
||||
YYGate();
|
||||
~YYGate() {}
|
||||
};
|
||||
|
||||
class ZZGate : public IntrinsicOneParaGate {
|
||||
|
@ -76,6 +87,8 @@ class ZZGate : public IntrinsicOneParaGate {
|
|||
|
||||
public:
|
||||
ZZGate(const Indexes &, const Indexes &, const ParameterResolver &);
|
||||
ZZGate();
|
||||
~ZZGate() {}
|
||||
};
|
||||
} // namespace mindquantum
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -43,6 +43,7 @@ class Hamiltonian {
|
|||
void Sparsed(bool);
|
||||
const Simulator::ComplexTermsDict &GetCTD() const;
|
||||
const Simulator::TermsDict &GetTD() const;
|
||||
~Hamiltonian() {}
|
||||
};
|
||||
} // namespace mindquantum
|
||||
} // namespace mindspore
|
||||
|
|
|
@ -32,6 +32,7 @@ class ParameterResolver {
|
|||
void SetData(const std::string &, const CalcType &);
|
||||
void InsertNoGrad(const std::string &);
|
||||
void InsertRequiresGrad(const std::string &);
|
||||
~ParameterResolver() {}
|
||||
|
||||
private:
|
||||
ParaType data_;
|
||||
|
|
|
@ -127,6 +127,9 @@ std::vector<std::vector<float>> PQCSimulator::CalcGradient(const std::shared_ptr
|
|||
if (dummy_circuit_) {
|
||||
continue;
|
||||
}
|
||||
if (len > circ_herm_gate_blocks.at(0).size()) {
|
||||
MS_LOG(EXCEPTION) << "hermitian circuit size error!";
|
||||
}
|
||||
for (unsigned i = 0; i < len; i++) {
|
||||
if ((!circ_herm_gate_blocks.at(0)[i]->IsParameterGate()) ||
|
||||
(circ_herm_gate_blocks.at(0)[i]->GetParameterResolver().GetRequiresGradParameters().size() == 0)) {
|
||||
|
|
|
@ -28,6 +28,7 @@ class Projector {
|
|||
explicit Projector(const NameType &);
|
||||
void HandleMask();
|
||||
Indexes GetMasks();
|
||||
~Projector() {}
|
||||
|
||||
private:
|
||||
NameType proj_str_;
|
||||
|
|
|
@ -62,7 +62,7 @@ SparseMatrix BasiGateSparse(char g) {
|
|||
|
||||
SparseMatrix IdentitySparse(int n_qubit) {
|
||||
if (n_qubit == 0) {
|
||||
int dim = 1UL << n_qubit;
|
||||
int dim = static_cast<int>(1UL << n_qubit);
|
||||
SparseMatrix out(dim, dim);
|
||||
out.reserve(VectorXi::Constant(dim, dim));
|
||||
for (int i = 0; i < dim; i++) {
|
||||
|
|
Loading…
Reference in New Issue