Maestro 0.2.5
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
QiskitAerState.h
Go to the documentation of this file.
1
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 std::vector<AER::SampleVector> samples =
110 state->sample_measure(qubits, shots, fakeState->rng_);
111 std::unordered_map<std::vector<bool>, uint_t> ret;
112 std::vector<bool> sample_vec(qubits.size());
113 for (const auto &sample : samples) {
114 for (size_t q = 0; q < qubits.size(); ++q) sample_vec[q] = sample[q] == 1;
115
116 ++ret[sample_vec];
117 }
118 return ret;
119 }
120};
121
122} // namespace Private
123} // namespace Simulators
124
125#endif
126#endif
127#endif