QNLP  v1.0
GateWriter.hpp
Go to the documentation of this file.
1 #ifndef QNLP_GATEWRITER
2 #define QNLP_GATEWRITER
3 
4 #include <iostream>
5 #include <fstream>
6 #include <unordered_set>
7 
8 namespace QNLP{
9  class GateWriter{
10  private:
11  std::ofstream latex_csv_out, matrix_val_out;
12  std::unordered_set<std::string> gates;
13 
14  public:
19  GateWriter() : GateWriter("qnlp_gate_calls.csv", "qnlp_matrices.log"){
20  }
21 
28  GateWriter( std::string latexCSVFilename, std::string matrixFilename ){
29  //Check if file exists, add header upon creation if not
30  std::ifstream file_exists(latexCSVFilename.c_str()) ;
31 
32  latex_csv_out.open ( latexCSVFilename, std::ofstream::out | std::ofstream::app );
33  matrix_val_out.open( matrixFilename, std::ofstream::out | std::ofstream::app );
34 
35  if (!(bool) file_exists)
36  latex_csv_out << "gate_label,gate_ctrl,gate_tgt,gate_value" << std::endl;
37  }
38 
44  latex_csv_out << "\n" << std::endl;
45  matrix_val_out << "\n" << std::endl;
46 
47  latex_csv_out.close();
48  matrix_val_out.close();
49  }
50 
56  void gateOut(std::string gateLabel, std::string matrixVals){
57  if( !gates.count(gateLabel)){
58  matrix_val_out << gateLabel << "," << matrixVals << std::endl;
59  gates.insert(gateLabel);
60  }
61  }
62 
68  void segmentMarkerOut(std::string segmentString){
69  latex_csv_out << "#!#{" << segmentString << "}#!#" << std::endl;
70  }
71 
79  void oneQubitGateCall(const std::string& gateLabel, const std::string& matrixVals, std::size_t gate_idx){
80  twoQubitGateCall(gateLabel, matrixVals, -1, gate_idx);
81  }
82 
91  void twoQubitGateCall(const std::string gateLabel, const std::string matrixVals, std::size_t gate_ctrl_idx, std::size_t gate_tgt_idx){
92  latex_csv_out << gateLabel << "," << gate_ctrl_idx << "," << gate_tgt_idx << "," << matrixVals << std::endl;
93  gateOut(gateLabel, matrixVals);
94  }
95 
101  std::unordered_set<std::string>& getAppliedGates(){
102  return gates;
103  }
104  };
105 }
106 
107 #endif
std::unordered_set< std::string > & getAppliedGates()
Return the map of all applied gates so far.
Definition: GateWriter.hpp:101
void gateOut(std::string gateLabel, std::string matrixVals)
Writes the given matrix to matrix_val_out. Assumes matrix type is printable.
Definition: GateWriter.hpp:56
~GateWriter()
Destroy the Gate Writer object.
Definition: GateWriter.hpp:43
std::ofstream latex_csv_out
Definition: GateWriter.hpp:11
GateWriter(std::string latexCSVFilename, std::string matrixFilename)
Construct a new Gate Writer object.
Definition: GateWriter.hpp:28
std::unordered_set< std::string > gates
Definition: GateWriter.hpp:12
void segmentMarkerOut(std::string segmentString)
Marks the output file with a string pattern to allow easy of parsing for gates to algorithm operation...
Definition: GateWriter.hpp:68
std::ofstream matrix_val_out
Definition: GateWriter.hpp:11
GateWriter()
Construct a new Gate Writer object.
Definition: GateWriter.hpp:19
void twoQubitGateCall(const std::string gateLabel, const std::string matrixVals, std::size_t gate_ctrl_idx, std::size_t gate_tgt_idx)
Writes the controlled gate to disk.
Definition: GateWriter.hpp:91
void oneQubitGateCall(const std::string &gateLabel, const std::string &matrixVals, std::size_t gate_idx)
Writes the single qubit gate to disk. -1 used to denote lack of control line.
Definition: GateWriter.hpp:79