Skip to content

Commit ce06ec1

Browse files
authored
Merge pull request #177 from OriginQ/develop
Develop
2 parents cc4a2df + 3412254 commit ce06ec1

File tree

111 files changed

+15087
-8031
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+15087
-8031
lines changed

Applications/B_V_Algorithm/BernsteinVaziraniAlgorithm.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
2-
Copyright (c) 2017-2020 Origin Quantum Computing. All Right Reserved.
2+
Copyright (c) 2017-2021 Origin Quantum Computing. All Right Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
6+
You may obtain a copy of the License at
77
88
http://www.apache.org/licenses/LICENSE-2.0
99

Applications/Grover_Algorithm/Grover_Algorithm.cpp

+21-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ limitations under the License.
2121
USING_QPANDA
2222
using namespace std;
2323

24-
static size_t g_shot = 100000;
24+
static size_t g_shot = 10000;
2525

2626
static uint32_t classcial_search(const std::vector<uint32_t>& search_space, const std::vector<uint32_t>& search_data,
2727
std::vector<size_t>& search_result)
@@ -47,7 +47,10 @@ static uint32_t quantum_grover_search(const std::vector<uint32_t>& search_space,
4747
uint32_t repeat_times = 0;
4848
auto machine = initQuantumMachine(CPU);
4949
machine->setConfigure({ 64,64 });
50-
auto x = machine->allocateCBit();
50+
51+
CPUQVM _tmp_qvm;
52+
_tmp_qvm.init();
53+
auto x = _tmp_qvm.allocateCBit();
5154

5255
std::vector<size_t> search_result_for_check;
5356
for (size_t i = 0; i < search_space.size(); ++i)
@@ -85,14 +88,20 @@ static uint32_t quantum_grover_search(const std::vector<uint32_t>& search_space,
8588
printf("Strat measure.\n");
8689
//auto result = probRunDict(grover_Qprog, measure_qubits);
8790
grover_Qprog << MeasureAll(measure_qubits, c);
91+
write_to_originir_file(grover_Qprog, machine, "Grover_prog.ir");
92+
93+
// just for test
94+
//auto _grover_prog = convert_originir_to_qprog("Grover_prog.ir", machine);
95+
//auto result = runWithConfiguration(_grover_prog, c, g_shot);
96+
8897
auto result = runWithConfiguration(grover_Qprog, c, g_shot);
8998

9099
//get result
91100
std::map<string, double> normal_result;
92101
for (const auto& _r : result){
93102
normal_result.insert(std::make_pair(_r.first, (double)_r.second/(double)g_shot));
94103
}
95-
search_result = search_target_from_measure_result(normal_result);
104+
search_result = search_target_from_measure_result(normal_result, measure_qubits.size());
96105
if ((search_result.size() > 0)
97106
|| ((search_result.size() == 0) && (max_repeat < repeat_times))){
98107
break;
@@ -180,7 +189,7 @@ static uint32_t quantum_walk_search(const std::vector<uint32_t>& search_space, c
180189
for (const auto& _r : result) {
181190
normal_result.insert(std::make_pair(_r.first, (double)_r.second / (double)g_shot));
182191
}
183-
search_result = search_target_from_measure_result(normal_result);
192+
search_result = search_target_from_measure_result(normal_result, measure_qubits.size());
184193
if ((search_result.size() > 0)
185194
|| ((search_result.size() == 0) && (4 < repeat_times))) {
186195
break;
@@ -260,16 +269,18 @@ int main(int argc, char* argv[])
260269
{
261270
const std::string parameter_descr_str = R"(
262271
The legal parameter form is as follows:
263-
QGrover.exe [Option] [search-space-file] [search-data]
272+
Grover_Algorithm.exe [Option] [search-space-file] [search-data]
264273
Option:
265274
-g: run Grover-search-algorithm
266275
-c: run classcial-search-algorithm
267276
-r: run quantum-walk-search-algorithm
268277
example:
269-
To search for 100 in data.txt use Grover-search-algorithm, execute the following command:
270-
Search.exe -g data.txt 100
278+
To search for 2 in data.txt use Grover-search-algorithm, execute the following command:
279+
Grover_Algorithm.exe -g data.txt 2
280+
data.txt:
281+
2,4,12,23,1,2,3,4,3,3,21,8,
271282
)";
272-
#if 1
283+
#if 0
273284
/* read param
274285
*/
275286
int search_type = -1; /**< 0: classcial-search-algorithm; 1:Grover-search-algorithm; 2:quantum-walk-search-algorithm */
@@ -314,8 +325,8 @@ int main(int argc, char* argv[])
314325
return -1;
315326
}
316327
#else
317-
int search_type = 2;
318-
std::string data_file = "data1.txt";
328+
int search_type = 1;
329+
std::string data_file = "F:\\data.txt";
319330
std::vector<uint32_t> search_data = {21};
320331
#endif
321332
/* run search algorithm

Core/QuantumCircuit/QGate.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "Core/Utilities/QProgInfo/ConfigMap.h"
33
#include "Core/Utilities/Tools/QPandaException.h"
44
#include <type_traits>
5+
#include "Core/Utilities/Tools/QStatMatrix.h"
6+
57
using namespace QGATE_SPACE;
68
using namespace std;
79
USING_QPANDA
@@ -604,7 +606,7 @@ QGate QPanda::U3(Qubit* qubit, double theta, double phi, double lambda)
604606

605607
QCircuit QPanda::U3(const QVec& qubits, double theta, double phi, double lambda)
606608
{
607-
string name = "RX";
609+
string name = "U3";
608610
QCircuit cir = QCircuit();
609611
for (auto &qubit : qubits)
610612
{
@@ -621,7 +623,7 @@ QGate QPanda::U3(Qubit* qubit, QStat& matrix)
621623

622624
QCircuit QPanda::U3(const QVec& qubits, QStat& matrix)
623625
{
624-
string name = "RX";
626+
string name = "U3";
625627
QCircuit cir = QCircuit();
626628
for (auto &qubit : qubits)
627629
{
@@ -2225,10 +2227,10 @@ QGate QPanda::Toffoli(int qaddr0, int qaddr1, int target_qaddr)
22252227

22262228
QGate QPanda::QOracle(const QVec &qubits, const QStat &matrix)
22272229
{
2228-
for (size_t i = 0; i < qubits.size() - 1; i++)
2229-
{
2230-
QPANDA_ASSERT(qubits[i]->get_phy_addr() >= qubits[i+1]->get_phy_addr(), "Error: QOracle qvec");
2231-
}
2230+
if (!is_unitary_matrix_by_eigen(matrix))
2231+
{
2232+
QCERR_AND_THROW_ERRSTR(invalid_argument, "Non-unitary matrix for QOracle-gate.");
2233+
}
22322234

22332235
auto value = matrix.size();
22342236
for (size_t i = 0; i < qubits.size(); i++)

Core/QuantumCircuit/QNode.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,21 @@ NodeIter NodeIter::getNextIter()
116116
}
117117
}
118118

119+
NodeIter NodeIter::getPreIter()
120+
{
121+
if (nullptr != m_pCur)
122+
{
123+
auto pItem = m_pCur->getPre();
124+
NodeIter temp(pItem);
125+
return temp;
126+
}
127+
else
128+
{
129+
NodeIter temp(nullptr);
130+
return temp;
131+
}
132+
}
133+
119134
bool NodeIter::operator!=(NodeIter iter) const
120135
{
121136
return this->m_pCur != iter.m_pCur;

Core/QuantumCircuit/QProgram.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,6 @@ size_t OriginProgram::get_qgate_num()
470470

471471
bool OriginProgram::is_measure_last_pos()
472472
{
473-
if (m_last_measure.empty())
474-
return false;
475-
476473
for (auto iter : m_last_measure)
477474
{
478475
if (iter.second == false)

Core/QuantumMachine/OriginQVM.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ limitations under the License.
2727
#include "Core/QuantumMachine/QProgExecution.h"
2828
#include "Core/Utilities/Tools/Uinteger.h"
2929
#include "Core/QuantumMachine/QProgCheck.h"
30+
#include "Core/Utilities/QProgInfo/QProgProgress.h"
3031
#include <set>
32+
#include <thread>
3133

3234

3335

@@ -462,7 +464,10 @@ void QVM::run(QProg& node)
462464
_pGates->initState(0, 1, node.get_max_qubit_addr() + 1);
463465

464466
QProgExecution prog_exec;
467+
_ExecId = uint64_t(&prog_exec);
468+
QProgProgress::getInstance().prog_start(_ExecId);
465469
prog_exec.execute(node.getImplementationPtr(), nullptr, config, _pGates);
470+
QProgProgress::getInstance().prog_end(_ExecId);
466471

467472
std::map<string, bool>result;
468473
prog_exec.get_return_value(result);
@@ -548,6 +553,15 @@ QResult* QVM::getResult()
548553

549554
void QVM::finalize()
550555
{
556+
if (nullptr != _AsyncTask)
557+
{
558+
// if( !_AsyncTask->is_finished())
559+
// {
560+
_AsyncTask->wait();
561+
// }
562+
delete _AsyncTask;
563+
}
564+
551565
if (nullptr != _Qubit_Pool)
552566
{
553567
delete _Qubit_Pool;
@@ -578,6 +592,8 @@ void QVM::finalize()
578592
_QResult = nullptr;
579593
_QMachineStatus = nullptr;
580594
_pGates = nullptr;
595+
_AsyncTask = nullptr;
596+
_ExecId = 0;
581597
}
582598

583599
size_t QVM::getAllocateQubit()
@@ -1057,6 +1073,26 @@ double QVM::get_expectation(QProg prog, const QHamiltonian& hamiltonian, const Q
10571073
return total_expectation;
10581074
}
10591075

1076+
size_t QVM::get_processed_qgate_num()
1077+
{
1078+
return _AsyncTask->get_process(QProgProgress::getInstance(), _ExecId);
1079+
}
1080+
1081+
void QVM::async_run(QProg& qProg)
1082+
{
1083+
_AsyncTask->run(this, qProg);
1084+
}
1085+
1086+
bool QVM::is_async_finished()
1087+
{
1088+
return _AsyncTask->is_finished();
1089+
}
1090+
1091+
std::map<std::string, bool> QVM::get_async_result()
1092+
{
1093+
return _AsyncTask->result();
1094+
}
1095+
10601096
static void accumulateProbability(prob_vec& probList, prob_vec& accumulateProb)
10611097
{
10621098
accumulateProb.clear();

0 commit comments

Comments
 (0)