class QuantumCircuit:
    
    def __init__(self, n_qubits, backend, shots):
        """用いる量子回路を定義"""
        # n_qubitsの量子回路を使用
        self._circuit = qiskit.QuantumCircuit(n_qubits)
        
        # n_qubits分の量子回路を定義
        all_qubits = [i for i in range(n_qubits)]
        # パラメーラθを定義
        self.theta = qiskit.circuit.Parameter('theta')
        
        # 全ての量子ビットにアダマール・ゲートを追加
        self._circuit.h(all_qubits)
        # 回路の一部を分離するバリアを追加
        self._circuit.barrier()
        # 全ての量子ビットに回転角θのY回転ゲートを追加
        self._circuit.ry(self.theta, all_qubits)        
        # 測定
        self._circuit.measure_all()

        self.backend = backend
        self.shots = shots
    
    def run(self, thetas):
        # 量子回路の計算を実行します
        job = qiskit.execute(self._circuit,self.backend,shots = self.shots,
                             parameter_binds = [{self.theta: theta} for theta in thetas])
        # 計算結果からカウント値を取得します
        result = job.result().get_counts(self._circuit)
        
        counts = np.array(list(result.values())) # カウント値です
        states = np.array(list(result.keys())).astype(float) # 状態です
        
        # おのおのの状態にたいする確率を計算
        probabilities = counts / self.shots
        # 期待値を計算
        expectation = np.sum(states * probabilities)
        
        return np.array([expectation])