QPanda

量子算法 Quantum Algorithm

用户接口 User Interface

量子机器模型 Quantum Machine Model

量子程序 Quantum Program

QPanda Framework

  • QPanda

QPanda Framework

量子算法 Quantum Algorithm

量子程序 Quantum Program

量子机器模型 Quantum Machine Model

用户接口 User Interface

支持控制流
Support Quantum Control Flow
轻松编写混合算法
Easy to make hybrid algorithm
完全使用经典语言
Enable full classical language
  • 支持控制流
    Support Quantum Control Flow
  • 轻松编写混合算法
    Easy to make hybrid algorithm
  • 完全使用经典语言
    Enable full classical language

:

#include "DJ_Algorithm.h"

QProg & Two_Qubit_DJ_Algorithm_Circuit(
	Qubit * qubit1, 
	Qubit * qubit2, 
	CBit * cbit, 
	vector< bool> oracle_function)

{
	auto &prog = CreateEmptyQProg();
	//Firstly, create a circuit container

	prog << H(qubit1) << H(qubit2);
	// Perform Hadamard gate on all qubits

	if (oracle_function[0] == false
		&&
		oracle_function[1] == false)
		// different oracle leads to different circuit
		// f(x) = oracle_function[x]
	{
		// f(x) = 0, do nothing
	}
	else if (oracle_function[0] == false
		&&
		oracle_function[1] == true
		)
	{
		// f(x) = x;
		prog << CNOT(qubit1, qubit2);
	}
	else if (oracle_function[0] == true
		&&
		oracle_function[1] == false
		)
	{
		// f(x) = x + 1;
		prog << RX(qubit2)
			<< CNOT(qubit1, qubit2)
			<< RX(qubit2);
	}
	else if (oracle_function[0] == true
		&&
		oracle_function[1] == true
		)
	{
		// f(x) = 1
		prog << RX(qubit2);
	}

	// Finally, Hadamard the first qubit and measure it
	prog << H(qubit1) << Measure(qubit1, cbit);
	return prog;
}
                                        
$O(\sqrt{N})$

#include "Grover_Algorithm.h"

QProg& Grover(vector<Qubit*> qVec, vector<CBit*> cVec, int target)
{
    QProg & grover = CreateEmptyQProg();
    OriginQCircuit & init = CreateEmptyCircuit();
    OriginQCircuit & oracle = CreateEmptyCircuit();
    OriginQCircuit & reverse = CreateEmptyCircuit();
    init << H(qVec[0]) << H(qVec[1]) << RX(qVec[2]) << H(qVec[2]);

    vector<Qubit *> controlVector;
    controlVector.push_back(qVec[0]);
    controlVector.push_back(qVec[1]);
    //QSingleGate  sqrtH(0.5*PI, 0, 0.25*PI, PI);
    OriginQGateNode  &toff = RX(qVec[2]);
    toff.setControl(controlVector);
    switch (target)
    {
    case 0:
        oracle << RX(qVec[0]) << RX(qVec[1]) << toff << RX(qVec[0]) << RX(qVec[1]);
        break;
    case 1:
        oracle << RX(qVec[0]) << toff << RX(qVec[0]);
        break;
    case 2:
        oracle << RX(qVec[1]) << toff << RX(qVec[1]);
        break;
    case 3:
        oracle << toff;
        break;
    }
    reverse << H(qVec[0]) << H(qVec[1]) << RX(qVec[0]) << RX(qVec[1])
        << H(qVec[1]) << CNOT(qVec[0], qVec[1]);
    reverse << H(qVec[1]) << RX(qVec[0]) << RX(qVec[1]) << H(qVec[0]) << H(qVec[1]) << RX(qVec[2]);
    grover << init << oracle << reverse << Measure(qVec[0], cVec[0]) << Measure(qVec[1], cVec[1]);
    return grover;
}
                                        

#include "HHL_Algorithm.h"

QProg& hhl
(vector<Qubit*> qVec,vector<CBit*> cVec)
{
    ClassicalCondition *cc0=bind_a_cbit(cVec[0]);

	// meaningless sentence
    OriginQCircuit & ifcircuit = CreateEmptyCircuit();

    OriginQCircuit & PSEcircuit = hhlPse(qVec);//PSE
    OriginQCircuit & CRot = CRotate(qVec);//control-lambda
    OriginQCircuit & PSEcircuitdag = hhlPse(qVec);
    //hhl circuit
    QProg & PSEdagger = CreateEmptyQProg();
    PSEdagger << PSEcircuitdag.dagger() << Measure(qVec[3], cVec[1]);
    QIfNode & ifnode = CreateIfProg(cc0, &PSEdagger);
    QProg & hhlProg = CreateEmptyQProg();
    //hhlProg << PSEcircuit <<CRot<<  Measure(qVec[0], cVec[0])<<ifnode;
    hhlProg << PSEcircuit << CRot << Measure(qVec[0], cVec[0]) << ifnode;
    return hhlProg;
}
OriginQCircuit& hhlPse(vector<Qubit*> qVec)
{
    OriginQCircuit & PSEcircuit = CreateEmptyCircuit();
    PSEcircuit << H(qVec[1]) << H(qVec[2]) << RZ(qVec[2], 0.75*PI);
    OriginQGateNode & gat1 = QDouble(PI, 1.5*PI, -0.5*PI, PI / 2, qVec[2], qVec[3]);
    OriginQGateNode  & gat2 = QDouble(PI, 1.5*PI, -PI, PI / 2, qVec[1], qVec[3]);
}

                                        

#include "Nonlocal_Game.h"

bool Winning_Test(pair<bool, bool> question, pair<bool, bool> answer)
{
	bool question_condition = question.first && question.second;
	bool answer_condition = answer.first || answer.second;

	if (question_condition == answer_condition)
	{
		return true; // Alice and Bob win the game.
	}
	else
	{
		return false; // Alice and Bob lose the game.
	}
}

pair<bool, bool> getRefereeQuestion()
{
	bool toAlice, toBob;
	srand(time(0));
	toAlice = static_cast<bool>(rand() % 2);
	toBob = static_cast<bool>(rand() % 2);
	// Random number simulation function is used to 
	// simulate the problem that Referee will ask Alice and Bob.
	return make_pair(toAlice, toBob);
}

QProg& Game(
	Qubit* alice_qubit,
	Qubit* bob_qubit,
	CBit* alice_meas,
	CBit* bob_meas,
	pair<bool, bool> &question)
{
	auto & game = CreateEmptyQProg();
	auto & entangled = CreateEmptyCircuit();
	auto & oracle = CreateEmptyCircuit();

	auto & Gate_I = RY(alice_qubit, 0);
	auto & Gate_T = RZ(bob_qubit, PI / 4);
	auto & Gate_T_Daga = RZ(bob_qubit, - PI / 4);
	// Custom Quantum logic Gates

	entangled << H(alice_qubit) << CNOT(bob_qubit, alice_qubit);
	// Make two qubit entangled, Here alice_qubit represent the qubit of Alice, and bob_qubit represent the qubit of bob. 
	question = getRefereeQuestion();

	if (question.first)
	{
		if (question.second)
		{
			oracle << H(alice_qubit) << S(bob_qubit) << H(bob_qubit) << Gate_T_Daga << H(bob_qubit);
		}
		else
		{
			oracle << H(alice_qubit) << S(bob_qubit) << H(bob_qubit) << Gate_T << H(bob_qubit);
		}
	}

	else
	{
		if (question.second)
		{
			oracle << Gate_I << S(bob_qubit) << H(bob_qubit) << Gate_T_Daga << H(bob_qubit);
		}
		else
		{
			oracle << Gate_I << S(bob_qubit) << H(bob_qubit) << Gate_T << H(bob_qubit);
		}
	}
	game << entangled << oracle << Measure(alice_qubit, alice_meas) << Measure(bob_qubit, bob_meas);
	return game;
}

void Nonlocal_Game(){
	size_t Round = 10;
	size_t win = 0;
	size_t lose = 0;
	for (size_t i = 0; i < Round; i++)
	{
		init();
		auto alice_qubit = qAlloc();
		auto   bob_qubit = qAlloc();
		auto  alice_meas = cAlloc();
		auto    bob_meas = cAlloc();
		pair<bool, bool> question;
		auto &GameProg = Game(alice_qubit,bob_qubit,alice_meas,bob_meas, question);
		load(GameProg);
		run();
		auto alice_answer = getCBitValue(alice_meas);
		auto   bob_answer = getCBitValue(bob_meas);
		bool final_result = Winning_Test(question, make_pair(alice_answer, bob_answer));
		if (final_result==true)
		{
			win++;
		}
		else
		{
			lose++;
		}
		cout << "The number of times player have won is;" << win;
		cout << "The number of times player have lose is;" << lose;
		finalize();

	}
}                                        

QPanda Framework
:2.0
:2018-06-04
:Windows Linux Mac版
量子软件开发包 QPanda
:1.0
:2018-06-04
:Windows版