Maestro 0.2.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
QiskitAerState.h
Go to the documentation of this file.
1
12
13#pragma once
14
15#ifndef _QISKIT_AER_STATE_H_
16#define _QISKIT_AER_STATE_H_
17
18#ifndef NO_QISKIT_AER
19
20#ifdef INCLUDED_BY_FACTORY
21
22#include "controllers/state_controller.hpp"
23
24namespace AER {
25
26class AerStateFake {
27 public:
28 virtual ~AerStateFake() = default;
29
30 bool initialized_;
31 uint_t num_of_qubits_;
32 RngEngine rng_;
33 int seed_ = std::random_device()();
34 std::shared_ptr<QuantumState::Base> state_;
35 json_t configs_;
36 ExperimentResult last_result_;
37};
38
39} // namespace AER
40
41namespace Simulators {
42// TODO: Maybe use the pimpl idiom
43// https://en.cppreference.com/w/cpp/language/pimpl to hide the implementation
44// for good but during development this should be good enough
45namespace Private {
46
55class QiskitAerState : public AER::AerState {
56 public:
57 const std::shared_ptr<AER::QuantumState::Base> &get_state() const {
58 const AER::AerStateFake *fakeState = (AER::AerStateFake *)(void *)this;
59 return fakeState->state_;
60 }
61
62 double expval_pauli(const reg_t &qubits, const std::string &pauli) {
63 if (qubits.empty() || pauli.empty()) return 1.;
64
65 const auto &state = get_state();
66 if (!state) return 0.;
67
68 flush_ops();
69
70 return state->expval_pauli(qubits, pauli);
71 }
72
73 std::vector<bool> apply_measure_many(const reg_t &qubits) {
74 const auto &state = get_state();
75 if (!state) return {};
76
77 flush_ops();
78
79 AER::Operations::Op op;
80 op.type = AER::Operations::OpType::measure;
81 op.name = "measure";
82 op.qubits = qubits;
83 op.memory = qubits;
84 op.registers = qubits;
85
86 AER::AerStateFake *fakeState = (AER::AerStateFake *)(void *)this;
87 fakeState->last_result_ = AER::ExperimentResult();
88 state->apply_op(op, fakeState->last_result_, fakeState->rng_);
89
90 std::vector<bool> bitstring(qubits.size(), false);
91 uint_t mem_size = state->creg().memory_size();
92 for (size_t q = 0; q < qubits.size(); ++q) {
93 const auto qubit = qubits[q];
94 if (state->creg().creg_memory()[mem_size - qubit - 1] == '1')
95 bitstring[q] = true;
96 }
97 return bitstring;
98 }
99
100 std::unordered_map<std::vector<bool>, uint_t> sample_counts_many(
101 const reg_t &qubits, uint_t shots) {
102 const auto &state = get_state();
103 if (!state) return {};
104
105 flush_ops();
106
107 AER::AerStateFake *fakeState = (AER::AerStateFake *)(void *)this;
108
109 // Aer's MPS backend returns samples sorted by ascending qubit index
110 // regardless of the order of `qubits` (see sort_measured_values in
111 // matrix_product_state_internal.cpp), while the statevector backend
112 // returns them in the order of `qubits`. To get consistent behavior
113 // across backends we sort the qubits ourselves and then remap the
114 // bits back to the caller-requested order.
115 reg_t sorted_qubits = qubits;
116 std::vector<size_t> order(qubits.size());
117 std::iota(order.begin(), order.end(), 0);
118 std::sort(order.begin(), order.end(),
119 [&qubits](size_t a, size_t b) { return qubits[a] < qubits[b]; });
120 for (size_t i = 0; i < qubits.size(); ++i)
121 sorted_qubits[i] = qubits[order[i]];
122
123 std::vector<AER::SampleVector> samples =
124 state->sample_measure(sorted_qubits, shots, fakeState->rng_);
125 std::unordered_map<std::vector<bool>, uint_t> ret;
126
127 std::vector<bool> bitstring(qubits.size());
128 for (const auto &sample : samples) {
129 for (size_t i = 0; i < qubits.size(); ++i)
130 bitstring[order[i]] = sample[i] == 1;
131
132 ++ret[bitstring];
133 }
134 return ret;
135 }
136};
137
138} // namespace Private
139} // namespace Simulators
140
141#endif
142#endif
143#endif