measurments.h 4.75 KB
Newer Older
Sebastian Wolf's avatar
Sebastian Wolf committed
1
// Xerus - A General Purpose Tensor Library
2
// Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf. 
Sebastian Wolf's avatar
Sebastian Wolf committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
// 
// Xerus is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// 
// Xerus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// 
// You should have received a copy of the GNU Affero General Public License
// along with Xerus. If not, see <http://www.gnu.org/licenses/>.
//
// For further information on Xerus visit https://libXerus.org 
// or contact us at contact@libXerus.org.

/**
 * @file
 * @brief Header file for the different measurment classes.
 */

#pragma once

Sebastian Wolf's avatar
Sebastian Wolf committed
27 28
#include <set>
#include <random>
Ben Huber's avatar
Ben Huber committed
29
#include <functional>
Sebastian Wolf's avatar
Sebastian Wolf committed
30

Sebastian Wolf's avatar
Sebastian Wolf committed
31
#include "basic.h"
32

Sebastian Wolf's avatar
Sebastian Wolf committed
33
#include "misc/math.h"
34
#include "misc/containerSupport.h"
Sebastian Wolf's avatar
Sebastian Wolf committed
35 36

namespace xerus {
37
	class Tensor;
38
	class TensorNetwork;
39
	
40 41 42
	/** 
	* @brief Class used to represent a single point measurments.
	*/
43
    class SinglePointMeasurementSet {
44 45 46
	public:
		std::vector<std::vector<size_t>> positions;
		std::vector<value_t> measuredValues;
47
		
48 49 50
		SinglePointMeasurementSet() = default;
		SinglePointMeasurementSet(const SinglePointMeasurementSet&  _other) = default;
		SinglePointMeasurementSet(      SinglePointMeasurementSet&& _other) = default;
51
		
52 53
		SinglePointMeasurementSet& operator=(const SinglePointMeasurementSet&  _other) = default;
		SinglePointMeasurementSet& operator=(      SinglePointMeasurementSet&& _other) = default;
Sebastian Wolf's avatar
Sebastian Wolf committed
54
		
55 56
		static SinglePointMeasurementSet random(const size_t _numMeasurements, const std::vector<size_t>& _dimensions);
		
57 58 59
		static SinglePointMeasurementSet random(const size_t _numMeasurements, const Tensor& _solution);
		
		static SinglePointMeasurementSet random(const size_t _numMeasurements, const TensorNetwork& _solution);
Sebastian Wolf's avatar
Sebastian Wolf committed
60
		
61 62
		static SinglePointMeasurementSet random(const size_t _numMeasurements, const std::vector<size_t>& _dimensions, std::function<value_t(const std::vector<size_t>&)> _callback);
		
Sebastian Wolf's avatar
Sebastian Wolf committed
63 64 65 66 67
		
		size_t size() const;
		
		size_t degree() const;
		
68 69 70 71
		value_t frob_norm() const;
		
		void add(std::vector<size_t> _position, const value_t _measuredValue);
		
72
		void sort(const bool _positionsOnly = false);
Sebastian Wolf's avatar
Sebastian Wolf committed
73
		
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
		
		void measure(const Tensor& _solution);
		
		void measure(const TensorNetwork& _solution);
		
		void measure(std::function<value_t(const std::vector<size_t>&)> _callback);
		
		
		double test(const Tensor& _solution) const;
		
		double test(const TensorNetwork& _solution) const;
		
		double test(std::function<value_t(const std::vector<size_t>&)> _callback) const;
		
		
	private:
		void create_random_positions(const size_t _numMeasurements, const std::vector<size_t>& _dimensions);
Sebastian Wolf's avatar
Sebastian Wolf committed
91 92 93
	};
	
	
94
	class RankOneMeasurementSet {
Sebastian Wolf's avatar
Sebastian Wolf committed
95
	public:
Sebastian Wolf's avatar
Sebastian Wolf committed
96
		std::vector<std::vector<Tensor>> positions;
97 98
		std::vector<value_t> measuredValues;
		
99 100 101
		RankOneMeasurementSet() = default;
		RankOneMeasurementSet(const RankOneMeasurementSet&  _other) = default;
		RankOneMeasurementSet(      RankOneMeasurementSet&& _other) = default;
102
		
103
		RankOneMeasurementSet(const SinglePointMeasurementSet&  _other, const std::vector<size_t> &_dimensions);
104

105 106
		RankOneMeasurementSet& operator=(const RankOneMeasurementSet&  _other) = default;
		RankOneMeasurementSet& operator=(      RankOneMeasurementSet&& _other) = default;
107
		
Sebastian Wolf's avatar
Sebastian Wolf committed
108 109 110 111 112 113 114 115
		static RankOneMeasurementSet random(const size_t _numMeasurements, const std::vector<size_t>& _dimensions);
		
		static RankOneMeasurementSet random(const size_t _numMeasurements, const Tensor& _solution);
		
		static RankOneMeasurementSet random(const size_t _numMeasurements, const TensorNetwork& _solution);
		
		static RankOneMeasurementSet random(const size_t _numMeasurements, const std::vector<size_t>& _dimensions, std::function<value_t(const std::vector<Tensor>&)> _callback);
		
Sebastian Wolf's avatar
Sebastian Wolf committed
116
		
Sebastian Wolf's avatar
Sebastian Wolf committed
117
		size_t size() const;
Sebastian Wolf's avatar
Sebastian Wolf committed
118
		
Sebastian Wolf's avatar
Sebastian Wolf committed
119 120
		size_t degree() const;
		
Sebastian Wolf's avatar
Sebastian Wolf committed
121 122 123 124 125 126
		value_t frob_norm() const;
		
		void add(const std::vector<Tensor>& _position, const value_t _measuredValue);
		
		void sort(const bool _positionsOnly);
		
127 128
		void normalize();
		
Sebastian Wolf's avatar
Sebastian Wolf committed
129 130 131 132 133
		
		void measure(const Tensor& _solution);
		
		void measure(const TensorNetwork& _solution);
		
134
		void measure(std::function<value_t(const std::vector<Tensor>&)> _callback);
Sebastian Wolf's avatar
Sebastian Wolf committed
135 136 137 138 139 140
		
		
		double test(const Tensor& _solution) const;
		
		double test(const TensorNetwork& _solution) const;
		
141
		double test(std::function<value_t(const std::vector<Tensor>&)> _callback) const;
Sebastian Wolf's avatar
Sebastian Wolf committed
142 143 144 145
		
		
	private:
		void create_random_positions(const size_t _numMeasurements, const std::vector<size_t>& _dimensions);
146
	};
Sebastian Wolf's avatar
Sebastian Wolf committed
147
	
Sebastian Wolf's avatar
Sebastian Wolf committed
148 149 150
	namespace internal {
		int comp(const Tensor& _a, const Tensor& _b);
	}
Sebastian Wolf's avatar
Sebastian Wolf committed
151
}