Skip to content

Commit

Permalink
Sparse qsp (#24)
Browse files Browse the repository at this point in the history
* fix bug

* first draft for sparse QSP using DD

* polish code

* change arbitrary sparse to uniform

* preparing sparse uniform quantum states

* sparse uniform quantum state works now

* create ADD and update files

* create sparse state from ADD

* fix problem in creating ADD

* create ADD: solve issue
  • Loading branch information
fmozafari committed May 23, 2022
1 parent 57ce6f3 commit de9d431
Show file tree
Hide file tree
Showing 8 changed files with 780 additions and 141 deletions.
2 changes: 1 addition & 1 deletion examples/qsp_using_bdd.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <angel/angel.hpp>
#include <angel/quantum_state_preparation/qsp_bdd.hpp>
#include <tweedledum/IR/Circuit.h>
#include <tweedledum/IR/Instruction.h>
#include <tweedledum/Utils/Visualization/string_utf8.h>
Expand Down
9 changes: 8 additions & 1 deletion examples/qsp_using_deps.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
#include <angel/angel.hpp>
#include <angel/quantum_state_preparation/qsp_deps.hpp>
#include <angel/dependency_analysis/esop_based_dependency_analysis.hpp>
#include <angel/dependency_analysis/pattern_based_dependency_analysis.hpp>
#include <angel/dependency_analysis/no_deps.hpp>
#include <angel/reordering/exhaustive_reordering.hpp>
#include <angel/reordering/greedy_reordering.hpp>
#include <angel/reordering/no_reordering.hpp>
#include <angel/reordering/random_reordering.hpp>
#include <tweedledum/IR/Circuit.h>
#include <tweedledum/IR/Instruction.h>
#include <tweedledum/Utils/Visualization/string_utf8.h>
Expand Down
1 change: 1 addition & 0 deletions include/angel/angel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <angel/dependency_analysis/no_deps.hpp>
#include <angel/quantum_state_preparation/qsp_deps.hpp>
#include <angel/quantum_state_preparation/qsp_bdd.hpp>
#include <angel/quantum_state_preparation/sparse_qsp.hpp>
#include <angel/reordering/exhaustive_reordering.hpp>
#include <angel/reordering/greedy_reordering.hpp>
#include <angel/reordering/no_reordering.hpp>
Expand Down
103 changes: 103 additions & 0 deletions include/angel/quantum_circuit/create_quantum_circuit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
namespace angel
{

struct target_qubit
{
uint32_t index;
enum gType {NOT, Ry};
gType gt;
double angle;
};

using MC_gates_t = std::map<uint32_t, std::vector<std::pair<double, std::vector<uint32_t>>>>;
using gates_sqs_t = std::vector<std::pair<target_qubit, std::vector<uint32_t>>>;

template<class Network>
inline void create_qc_for_MCgates( Network& qc, MC_gates_t gates, std::vector<uint32_t> order )
Expand Down Expand Up @@ -57,4 +66,98 @@ inline void create_qc_for_MCgates( Network& qc, MC_gates_t gates, std::vector<ui
}
}

template<class Network>
inline void create_qc_for_sparse_uqsp( Network& qc, gates_sqs_t gates, uint32_t q_count, uint64_t & cnot_count)
{
uint32_t num_mcg = 0;
uint32_t sum_controls = 0;
std::vector<tweedledum::Qubit> q;
std::vector<tweedledum::Cbit> c;

/* create qubits and cbits */
for ( auto i = 0u; i < q_count; i++ )
{
q.push_back( qc.create_qubit() );
c.push_back( qc.create_cbit() );
}

qc.apply_operator( tweedledum::Op::X(), {q[q_count-1]} );
gates.pop_back(); /* last computation for ancilla qubit doesn't need
*/

/*for(auto [target, controls]: gates)
{
std::cout<<"target: "<<target.index<<" controls: ";
for ( auto j = 0u; j < controls.size(); j++ )
{
std::cout<<" "<<controls[j];
}
std::cout<<std::endl;
}*/

for(auto [target, controls]: gates)
{
std::vector<tweedledum::Qubit> qlines;
for ( auto j = 0u; j < controls.size(); j++ ) /* insert control qubits */
{
auto idx = controls[j];
if ( idx % 2 == 0u )
qlines.push_back( q[idx / 2 - 1] );
else
qlines.push_back( !q[idx / 2 - 1] );
}
qlines.push_back( q[target.index - 1] ); /* insert targt qubit */

if(target.gt == target_qubit::NOT)
qc.apply_operator( tweedledum::Op::X(), qlines );
else
qc.apply_operator( tweedledum::Op::Ry( target.angle ), qlines );
if(controls.size()==1 && (target.gt == target_qubit::NOT))
cnot_count += 1;
else if(controls.size()==0)
cnot_count += 0;
else if(controls.size()==1)
{
cnot_count += 2;
num_mcg ++;
sum_controls += 1;
}
else if(controls.size()==2)
{
cnot_count += 6;
num_mcg ++;
sum_controls += 2;
}
else if(controls.size()==3)
{
cnot_count += (8*6+2);
num_mcg ++;
sum_controls += 3;
}

else if(controls.size()==4)
{
cnot_count += 10*6;
num_mcg ++;
sum_controls += 4;
}
else
{
auto c_part1 = floor(controls.size()/2);
auto c_part2 = controls.size() - c_part1;
auto count1 = 2 * (2*(c_part1-1)+2*(c_part1-2));
auto count2 = 2 * (2*(c_part2-1)+2*(c_part2-2));
cnot_count += (count1+count2);
num_mcg ++;
sum_controls += controls.size();
}



}

std::cout<<"average number of branches: "<<sum_controls/num_mcg<<std::endl;

}

} // namespace angel

0 comments on commit de9d431

Please sign in to comment.