15#ifndef _QISKIT_AER_STATE_H_
16#define _QISKIT_AER_STATE_H_
20#ifdef INCLUDED_BY_FACTORY
22#include "controllers/state_controller.hpp"
28 virtual ~AerStateFake() =
default;
31 uint_t num_of_qubits_;
33 int seed_ = std::random_device()();
34 std::shared_ptr<QuantumState::Base> state_;
36 ExperimentResult last_result_;
55class QiskitAerState :
public AER::AerState {
57 const std::shared_ptr<AER::QuantumState::Base> &get_state()
const {
58 const AER::AerStateFake *fakeState = (AER::AerStateFake *)(
void *)
this;
59 return fakeState->state_;
62 double expval_pauli(
const reg_t &qubits,
const std::string &pauli) {
63 if (qubits.empty() || pauli.empty())
return 1.;
65 const auto &state = get_state();
66 if (!state)
return 0.;
70 return state->expval_pauli(qubits, pauli);
73 std::vector<bool> apply_measure_many(
const reg_t &qubits) {
74 const auto &state = get_state();
75 if (!state)
return {};
79 AER::Operations::Op op;
80 op.type = AER::Operations::OpType::measure;
84 op.registers = qubits;
86 AER::AerStateFake *fakeState = (AER::AerStateFake *)(
void *)
this;
87 fakeState->last_result_ = AER::ExperimentResult();
88 state->apply_op(op, fakeState->last_result_, fakeState->rng_);
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')
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 {};
107 AER::AerStateFake *fakeState = (AER::AerStateFake *)(
void *)
this;
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;