GCC Code Coverage Report


Directory: ./
File: MeasurementSetup/include/MeasurementSetup/MeasurementSetup.hpp
Date: 2022-10-15 05:10:18
Exec Total Coverage
Lines: 6 10 60.0%
Functions: 5 8 62.5%
Branches: 0 0 -%
Decisions: 0 0 -%

Line Branch Decision Exec Source
1 // Copyright 2019-2022 Cambridge Quantum Computing
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #pragma once
16
17 #include "Circuit/Circuit.hpp"
18 #include "Utils/Json.hpp"
19 #include "Utils/PauliStrings.hpp"
20
21 namespace tket {
22
23 /**
24 * MeasurementSetup
25 * Captures an expectation value experiment.
26 * Given a collection of Pauli terms, the setup contains a collection
27 * of measurement circuits from which each Pauli measurement can be extracted.
28 *
29 * The result_map maps Pauli terms to circuits from which the expectation value
30 * can be obtained, along with the classical post-processing required on each
31 * shot (which bits/columns of the shot table to XOR together, and whether to
32 * invert the final value).
33 *
34 * There may be multiple measurement circuits that measure the same Pauli term,
35 * so we have a vector of MeasurementBitMaps for each term to exploit this and
36 * effectively get extra shots for free.
37 */
38 class MeasurementSetup {
39 public:
40 struct MeasurementBitMap {
41 MeasurementBitMap() : circ_index{0}, invert{false} {}
42 MeasurementBitMap(
43 unsigned _circ_index, const std::vector<unsigned> &_bits,
44 bool _invert = 0)
45 : circ_index{_circ_index}, bits{_bits}, invert{_invert} {}
46
47 unsigned circ_index;
48 std::vector<unsigned> bits;
49 bool invert;
50
51 18 const unsigned &get_circ_index() const { return circ_index; }
52 18 const std::vector<unsigned> &get_bits() const { return bits; }
53 18 const bool &get_invert() const { return invert; }
54
55 std::string to_str() const;
56 };
57 struct QPSHasher {
58 177 std::size_t operator()(const QubitPauliString &qps) const {
59 177 return hash_value(qps);
60 }
61 };
62 typedef std::unordered_map<
63 QubitPauliString, std::vector<MeasurementBitMap>, QPSHasher>
64 measure_result_map_t;
65
66 const std::vector<Circuit> &get_circs() const { return measurement_circs; }
67 4 const measure_result_map_t &get_result_map() const { return result_map; }
68
69 void add_measurement_circuit(const Circuit &circ);
70 void add_result_for_term(
71 const QubitPauliString &term, const MeasurementBitMap &result);
72 /**
73 * Rejects the coefficient in the QubitPauliTensor. Used for
74 * convenience at the C++ level, and should not be exposed.
75 */
76 void add_result_for_term(
77 const QubitPauliTensor &term, const MeasurementBitMap &result);
78 /**
79 * Checks that the tensors to be measured correspond to the
80 * correct tensors generated by the measurement circs. Includes
81 * parity checks.
82 */
83 bool verify() const;
84
85 std::string to_str() const;
86
87 private:
88 /**
89 * Clifford circuits to generate the correct Pauli measurements,
90 * with Measure gates on the required qubits.
91 */
92 std::vector<Circuit> measurement_circs;
93 measure_result_map_t result_map;
94 };
95
96 JSON_DECL(MeasurementSetup::MeasurementBitMap)
97 JSON_DECL(MeasurementSetup)
98
99 } // namespace tket
100