code: # qubit number=3
  # total number=60
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  
  def build_oracle(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename=(kernel + '-oracle.png'))
      return oracle
  
  
  def build_circuit(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the Bernstein-Vazirani circuit
      zero = np.binary_repr(0, n)
      b = f(zero)
  
      # initial n + 1 bits
      input_qubit = QuantumRegister(n+1, "qc")
      classicals = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classicals)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(input_qubit[n])
      # circuit begin
      prog.h(input_qubit[1])  # number=1
      prog.h(input_qubit[2]) # number=38
      prog.cz(input_qubit[0],input_qubit[2]) # number=39
      prog.h(input_qubit[2]) # number=40
      prog.cx(input_qubit[0],input_qubit[2]) # number=31
      prog.h(input_qubit[2]) # number=42
      prog.cz(input_qubit[0],input_qubit[2]) # number=43
      prog.h(input_qubit[2]) # number=44
      prog.h(input_qubit[2]) # number=48
      prog.cz(input_qubit[0],input_qubit[2]) # number=49
      prog.h(input_qubit[2]) # number=50
      prog.h(input_qubit[2]) # number=57
      prog.cz(input_qubit[0],input_qubit[2]) # number=58
      prog.h(input_qubit[2]) # number=59
      prog.x(input_qubit[2]) # number=55
      prog.cx(input_qubit[0],input_qubit[2]) # number=56
      prog.cx(input_qubit[0],input_qubit[2]) # number=47
      prog.cx(input_qubit[0],input_qubit[2]) # number=37
      prog.h(input_qubit[2]) # number=51
      prog.cz(input_qubit[0],input_qubit[2]) # number=52
      prog.h(input_qubit[2]) # number=53
      prog.h(input_qubit[2]) # number=25
      prog.cz(input_qubit[0],input_qubit[2]) # number=26
      prog.h(input_qubit[2]) # number=27
      prog.h(input_qubit[1]) # number=7
      prog.cz(input_qubit[2],input_qubit[1]) # number=8
      prog.rx(0.17592918860102857,input_qubit[2]) # number=34
      prog.rx(-0.3989822670059037,input_qubit[1]) # number=30
      prog.h(input_qubit[1]) # number=9
      prog.h(input_qubit[1]) # number=18
      prog.cz(input_qubit[2],input_qubit[1]) # number=19
      prog.h(input_qubit[1]) # number=20
      prog.y(input_qubit[1]) # number=14
      prog.h(input_qubit[1]) # number=22
      prog.cz(input_qubit[2],input_qubit[1]) # number=23
      prog.h(input_qubit[1]) # number=24
      prog.z(input_qubit[2]) # number=3
      prog.z(input_qubit[1]) # number=41
      prog.x(input_qubit[1]) # number=17
      prog.y(input_qubit[2]) # number=5
      prog.x(input_qubit[2]) # number=21
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
      prog.h(input_qubit[n])
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [input_qubit[n]])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      return prog
  
  
  def get_statevector(prog: QuantumCircuit) -> Any:
      state_backend = Aer.get_backend('statevector_simulator')
      statevec = execute(prog, state_backend).result()
      quantum_state = statevec.get_statevector()
      qubits = round(log2(len(quantum_state)))
      quantum_state = {
          "|" + np.binary_repr(i, qubits) + ">": quantum_state[i]
          for i in range(2 ** qubits)
      }
      return quantum_state
  
  
  def evaluate(backend_str: str, prog: QuantumCircuit, shots: int, b: str) -> Any:
      # Q: which backend should we use?
  
      # get state vector
      quantum_state = get_statevector(prog)
  
      # get simulate results
  
      # provider = IBMQ.load_account()
      # backend = provider.get_backend(backend_str)
      # qobj = compile(prog, backend, shots)
      # job = backend.run(qobj)
      # job.result()
      backend = Aer.get_backend(backend_str)
      # transpile/schedule -> assemble -> backend.run
      results = execute(prog, backend, shots=shots).result()
      counts = results.get_counts()
      a = Counter(counts).most_common(1)[0][0][::-1]
  
      return {
          "measurements": counts,
          # "state": statevec,
          "quantum_state": quantum_state,
          "a": a,
          "b": b
      }
  
  
  def bernstein_test_1(rep: str):
      """011 . x + 1"""
      a = "011"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_2(rep: str):
      """000 . x + 0"""
      a = "000"
      b = "0"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_3(rep: str):
      """111 . x + 1"""
      a = "111"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  if __name__ == "__main__":
      n = 2
      a = "11"
      b = "1"
      f = lambda rep: \
          bitwise_xor(bitwise_dot(a, rep), b)
      prog = build_circuit(n, f)
      sample_shot =4000
      writefile = open("../data/startQiskit289.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      backend = BasicAer.get_backend('qasm_simulator')
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.h(qubit=2)
      circuit1.x(qubit=3)
      circuit1.measure_all()
  
      info = execute(circuit1,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=4
  # total number=40
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=31
      prog.cz(input_qubit[0],input_qubit[3]) # number=32
      prog.h(input_qubit[3]) # number=33
      prog.x(input_qubit[3]) # number=27
      prog.h(input_qubit[3]) # number=34
      prog.cz(input_qubit[0],input_qubit[3]) # number=35
      prog.h(input_qubit[3]) # number=36
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.cx(input_qubit[3],input_qubit[0]) # number=37
      prog.z(input_qubit[3]) # number=38
      prog.cx(input_qubit[3],input_qubit[0]) # number=39
      prog.h(input_qubit[3]) # number=4
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.cx(input_qubit[2],input_qubit[0]) # number=10
      prog.h(input_qubit[0]) # number=14
      prog.h(input_qubit[1]) # number=30
      prog.cz(input_qubit[2],input_qubit[0]) # number=15
      prog.h(input_qubit[0]) # number=16
      prog.cx(input_qubit[0],input_qubit[2]) # number=20
      prog.x(input_qubit[2]) # number=21
      prog.cx(input_qubit[0],input_qubit[2]) # number=22
      prog.cx(input_qubit[0],input_qubit[2]) # number=17
      prog.cx(input_qubit[0],input_qubit[2]) # number=23
      prog.x(input_qubit[2]) # number=24
      prog.cx(input_qubit[0],input_qubit[2]) # number=25
      prog.cx(input_qubit[0],input_qubit[2]) # number=19
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = BasicAer.get_backend('qasm_simulator')
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit2375.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=84
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  
  def build_oracle(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename=(kernel + '-oracle.png'))
      return oracle
  
  
  def build_circuit(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the Bernstein-Vazirani circuit
      zero = np.binary_repr(0, n)
      b = f(zero)
  
      # initial n + 1 bits
      input_qubit = QuantumRegister(n+1, "qc")
      classicals = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classicals)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(input_qubit[n])
      # circuit begin
      prog.h(input_qubit[1])  # number=1
      prog.h(input_qubit[1]) # number=70
      prog.rx(-0.09738937226128368,input_qubit[2]) # number=2
      prog.h(input_qubit[1]) # number=33
      prog.y(input_qubit[2]) # number=56
      prog.cz(input_qubit[2],input_qubit[1]) # number=34
      prog.h(input_qubit[1]) # number=35
      prog.h(input_qubit[1]) # number=3
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
      prog.h(input_qubit[n])
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [input_qubit[n]])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      return prog
  
  
  def get_statevector(prog: QuantumCircuit) -> Any:
      state_backend = Aer.get_backend('statevector_simulator')
      statevec = execute(prog, state_backend).result()
      quantum_state = statevec.get_statevector()
      qubits = round(log2(len(quantum_state)))
      quantum_state = {
          "|" + np.binary_repr(i, qubits) + ">": quantum_state[i]
          for i in range(2 ** qubits)
      }
      return quantum_state
  
  
  def evaluate(backend_str: str, prog: QuantumCircuit, shots: int, b: str) -> Any:
      # Q: which backend should we use?
  
      # get state vector
      quantum_state = get_statevector(prog)
  
      # get simulate results
  
      # provider = IBMQ.load_account()
      # backend = provider.get_backend(backend_str)
      # qobj = compile(prog, backend, shots)
      # job = backend.run(qobj)
      # job.result()
      backend = Aer.get_backend(backend_str)
      # transpile/schedule -> assemble -> backend.run
      results = execute(prog, backend, shots=shots).result()
      counts = results.get_counts()
      a = Counter(counts).most_common(1)[0][0][::-1]
  
      return {
          "measurements": counts,
          # "state": statevec,
          "quantum_state": quantum_state,
          "a": a,
          "b": b
      }
  
  
  def bernstein_test_1(rep: str):
      """011 . x + 1"""
      a = "011"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_2(rep: str):
      """000 . x + 0"""
      a = "000"
      b = "0"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_3(rep: str):
      """111 . x + 1"""
      a = "111"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  if __name__ == "__main__":
      n = 2
      a = "11"
      b = "1"
      f = lambda rep: \
          bitwise_xor(bitwise_dot(a, rep), b)
      prog = build_circuit(n, f)
      sample_shot =4000
      writefile = open("../data/startQiskit_QC456.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = provider.get_backend("ibmq_belem")
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.h(qubit=2)
      circuit1.x(qubit=3)
      circuit1.measure_all()
  
      info = execute(circuit1,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=4
  # total number=33
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.cx(input_qubit[0],input_qubit[3]) # number=13
      prog.cx(input_qubit[0],input_qubit[3]) # number=17
      prog.x(input_qubit[3]) # number=18
      prog.rx(-3.1101767270538954,input_qubit[1]) # number=27
      prog.cx(input_qubit[0],input_qubit[3]) # number=19
      prog.cx(input_qubit[0],input_qubit[3]) # number=15
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.y(input_qubit[3]) # number=12
      prog.h(input_qubit[1]) # number=26
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.x(input_qubit[3]) # number=29
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[0]) # number=30
      prog.cz(input_qubit[3],input_qubit[0]) # number=31
      prog.h(input_qubit[0]) # number=32
      prog.cx(input_qubit[3],input_qubit[0]) # number=23
      prog.z(input_qubit[3]) # number=24
      prog.cx(input_qubit[3],input_qubit[0]) # number=25
      prog.cx(input_qubit[3],input_qubit[0]) # number=22
      prog.h(input_qubit[3])  # number=8
      prog.z(input_qubit[3]) # number=28
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[2]) # number=10
      prog.y(input_qubit[2]) # number=11
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = BasicAer.get_backend('statevector_simulator')
      sample_shot =8000
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_Class2296.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=10
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  import networkx as nx
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def make_circuit(n:int) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      prog = QuantumCircuit(input_qubit)
      prog.h(input_qubit[0]) # number=1
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2])  # number=3
      prog.h(input_qubit[3])  # number=4
      prog.y(input_qubit[3]) # number=5
  
      for edge in E:
          k = edge[0]
          l = edge[1]
          prog.cp(-2 * gamma, input_qubit[k-1], input_qubit[l-1])
          prog.p(gamma, k)
          prog.p(gamma, l)
  
      prog.rx(2 * beta, range(len(V)))
  
      prog.swap(input_qubit[1],input_qubit[0]) # number=6
      prog.swap(input_qubit[1],input_qubit[0]) # number=7
      prog.y(input_qubit[3]) # number=8
      prog.y(input_qubit[3]) # number=9
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      n = 4
      V = np.arange(0, n, 1)
      E = [(0, 1, 1.0), (0, 2, 1.0), (1, 2, 1.0), (3, 2, 1.0), (3, 1, 1.0)]
  
      G = nx.Graph()
      G.add_nodes_from(V)
      G.add_weighted_edges_from(E)
  
      step_size = 0.1
  
      a_gamma = np.arange(0, np.pi, step_size)
      a_beta = np.arange(0, np.pi, step_size)
      a_gamma, a_beta = np.meshgrid(a_gamma, a_beta)
  
      F1 = 3 - (np.sin(2 * a_beta) ** 2 * np.sin(2 * a_gamma) ** 2 - 0.5 * np.sin(4 * a_beta) * np.sin(4 * a_gamma)) * (
                  1 + np.cos(4 * a_gamma) ** 2)
  
      result = np.where(F1 == np.amax(F1))
      a = list(zip(result[0], result[1]))[0]
  
      gamma = a[0] * step_size
      beta = a[1] * step_size
  
      prog = make_circuit(4)
      sample_shot =5600
      writefile = open("../data/startQiskit_noisy62.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      backend = FakeYorktown()
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.measure_all()
      prog = circuit1
  
      info = execute(prog,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=4
  # total number=36
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.cx(input_qubit[0],input_qubit[3]) # number=13
      prog.cx(input_qubit[0],input_qubit[3]) # number=17
      prog.x(input_qubit[3]) # number=18
      prog.cx(input_qubit[0],input_qubit[3]) # number=19
      prog.cx(input_qubit[0],input_qubit[3]) # number=15
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.y(input_qubit[3]) # number=12
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.cx(input_qubit[0],input_qubit[3]) # number=27
      prog.x(input_qubit[3]) # number=28
      prog.h(input_qubit[3]) # number=30
      prog.cz(input_qubit[0],input_qubit[3]) # number=31
      prog.h(input_qubit[3]) # number=32
      prog.cx(input_qubit[3],input_qubit[0]) # number=20
      prog.h(input_qubit[0]) # number=33
      prog.cz(input_qubit[3],input_qubit[0]) # number=34
      prog.h(input_qubit[0]) # number=35
      prog.z(input_qubit[3]) # number=24
      prog.cx(input_qubit[3],input_qubit[0]) # number=25
      prog.cx(input_qubit[3],input_qubit[0]) # number=22
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[2]) # number=10
      prog.y(input_qubit[2]) # number=11
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = FakeVigo()
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_noisy2042.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=42
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=35
      prog.cz(input_qubit[0],input_qubit[3]) # number=36
      prog.h(input_qubit[3]) # number=37
      prog.h(input_qubit[3]) # number=22
      prog.cx(input_qubit[0],input_qubit[3]) # number=32
      prog.x(input_qubit[3]) # number=33
      prog.cx(input_qubit[0],input_qubit[3]) # number=34
      prog.h(input_qubit[3]) # number=19
      prog.cz(input_qubit[0],input_qubit[3]) # number=20
      prog.h(input_qubit[3]) # number=21
      prog.z(input_qubit[3]) # number=10
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
      prog.h(input_qubit[0]) # number=26
      prog.cz(input_qubit[1],input_qubit[0]) # number=27
      prog.h(input_qubit[0]) # number=28
      prog.z(input_qubit[1]) # number=24
      prog.h(input_qubit[2]) # number=39
      prog.cz(input_qubit[3],input_qubit[2]) # number=40
      prog.h(input_qubit[2]) # number=41
      prog.h(input_qubit[0]) # number=29
      prog.cz(input_qubit[1],input_qubit[0]) # number=30
      prog.h(input_qubit[0]) # number=31
      prog.h(input_qubit[1]) # number=18
      prog.rx(2.8902652413026093,input_qubit[2]) # number=13
  
      prog.y(input_qubit[1]) # number=11
      prog.y(input_qubit[1]) # number=12
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = BasicAer.get_backend('qasm_simulator')
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit2925.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=39
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=36
      prog.cz(input_qubit[0],input_qubit[3]) # number=37
      prog.h(input_qubit[3]) # number=38
      prog.h(input_qubit[3]) # number=23
      prog.cz(input_qubit[0],input_qubit[3]) # number=24
      prog.h(input_qubit[3]) # number=25
      prog.x(input_qubit[3]) # number=18
      prog.cx(input_qubit[0],input_qubit[3]) # number=19
      prog.cx(input_qubit[0],input_qubit[3]) # number=15
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.y(input_qubit[3]) # number=12
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3]) # number=32
      prog.cx(input_qubit[3],input_qubit[0]) # number=20
      prog.cx(input_qubit[3],input_qubit[0]) # number=26
      prog.z(input_qubit[3]) # number=27
      prog.h(input_qubit[0]) # number=29
      prog.cz(input_qubit[3],input_qubit[0]) # number=30
      prog.h(input_qubit[0]) # number=31
      prog.h(input_qubit[0]) # number=33
      prog.cz(input_qubit[3],input_qubit[0]) # number=34
      prog.h(input_qubit[0]) # number=35
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[2]) # number=10
      prog.y(input_qubit[2]) # number=11
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = BasicAer.get_backend('statevector_simulator')
      sample_shot =8000
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_Class2285.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=36
  import cirq
  import qiskit
  from qiskit import IBMQ
  from qiskit.providers.ibmq import least_busy
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=15
      prog.cz(input_qubit[0],input_qubit[3]) # number=16
      prog.h(input_qubit[3]) # number=17
      prog.x(input_qubit[3]) # number=13
      prog.h(input_qubit[3]) # number=20
      prog.cz(input_qubit[0],input_qubit[3]) # number=21
      prog.h(input_qubit[3]) # number=22
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.h(input_qubit[0]) # number=5
      prog.cx(input_qubit[0],input_qubit[3]) # number=33
      prog.x(input_qubit[3]) # number=34
      prog.cx(input_qubit[0],input_qubit[3]) # number=35
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[1]) # number=29
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.h(input_qubit[0]) # number=23
      prog.cz(input_qubit[2],input_qubit[0]) # number=24
      prog.h(input_qubit[0]) # number=25
      prog.y(input_qubit[2]) # number=30
      prog.cx(input_qubit[2],input_qubit[0]) # number=11
      prog.cx(input_qubit[2],input_qubit[0]) # number=18
      prog.h(input_qubit[0]) # number=26
      prog.x(input_qubit[2]) # number=31
      prog.cz(input_qubit[2],input_qubit[0]) # number=27
      prog.h(input_qubit[0]) # number=28
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2 and not x.configuration().simulator and x.status().operational == True))
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_QC2348.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=9
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.h(input_qubit[1]) # number=4
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      prog.x(input_qubit[1]) # number=2
      prog.x(input_qubit[1]) # number=3
      prog.cx(input_qubit[1],input_qubit[0]) # number=5
      prog.cx(input_qubit[1],input_qubit[0]) # number=6
      prog.cx(input_qubit[1],input_qubit[0]) # number=7
      prog.cx(input_qubit[1],input_qubit[0]) # number=8
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = BasicAer.get_backend('statevector_simulator')
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      prog = circuit1
  
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
  
      writefile = open("../data/startQiskit_Class124.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=13
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      prog.y(input_qubit[1]) # number=2
      prog.y(input_qubit[1]) # number=4
      prog.y(input_qubit[1]) # number=3
      prog.cx(input_qubit[1],input_qubit[0]) # number=7
      prog.x(input_qubit[0]) # number=8
      prog.h(input_qubit[0]) # number=10
      prog.cz(input_qubit[1],input_qubit[0]) # number=11
      prog.h(input_qubit[0]) # number=12
      prog.x(input_qubit[0]) # number=6
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = BasicAer.get_backend('statevector_simulator')
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      prog = circuit1
  
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
  
      writefile = open("../data/startQiskit_Class182.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=15
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  import networkx as nx
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def make_circuit(n:int) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      prog = QuantumCircuit(input_qubit)
      prog.h(input_qubit[0]) # number=1
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2])  # number=3
      prog.cx(input_qubit[0],input_qubit[2]) # number=9
      prog.x(input_qubit[2]) # number=10
      prog.h(input_qubit[2]) # number=12
      prog.cz(input_qubit[0],input_qubit[2]) # number=13
      prog.h(input_qubit[2]) # number=14
      prog.h(input_qubit[3])  # number=4
      prog.y(input_qubit[3]) # number=5
  
      for edge in E:
          k = edge[0]
          l = edge[1]
          prog.cp(-2 * gamma, input_qubit[k-1], input_qubit[l-1])
          prog.p(gamma, k)
          prog.p(gamma, l)
  
      prog.rx(2 * beta, range(len(V)))
  
      prog.cx(input_qubit[1],input_qubit[0]) # number=7
      prog.cx(input_qubit[1],input_qubit[0]) # number=8
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      n = 4
      V = np.arange(0, n, 1)
      E = [(0, 1, 1.0), (0, 2, 1.0), (1, 2, 1.0), (3, 2, 1.0), (3, 1, 1.0)]
  
      G = nx.Graph()
      G.add_nodes_from(V)
      G.add_weighted_edges_from(E)
  
      step_size = 0.1
  
      a_gamma = np.arange(0, np.pi, step_size)
      a_beta = np.arange(0, np.pi, step_size)
      a_gamma, a_beta = np.meshgrid(a_gamma, a_beta)
  
      F1 = 3 - (np.sin(2 * a_beta) ** 2 * np.sin(2 * a_gamma) ** 2 - 0.5 * np.sin(4 * a_beta) * np.sin(4 * a_gamma)) * (
                  1 + np.cos(4 * a_gamma) ** 2)
  
      result = np.where(F1 == np.amax(F1))
      a = list(zip(result[0], result[1]))[0]
  
      gamma = a[0] * step_size
      beta = a[1] * step_size
  
      prog = make_circuit(4)
      sample_shot =5600
      writefile = open("../data/startQiskit_QC277.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = provider.get_backend("ibmq_5_yorktown")
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.measure_all()
      prog = circuit1
  
      info = execute(prog,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=4
  # total number=49
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.cx(input_qubit[0],input_qubit[3]) # number=13
      prog.cx(input_qubit[0],input_qubit[3]) # number=17
      prog.x(input_qubit[3]) # number=18
      prog.cx(input_qubit[0],input_qubit[3]) # number=19
      prog.cx(input_qubit[0],input_qubit[3]) # number=15
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.y(input_qubit[3]) # number=12
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3]) # number=37
      prog.cz(input_qubit[0],input_qubit[3]) # number=38
      prog.h(input_qubit[3]) # number=39
      prog.cx(input_qubit[0],input_qubit[3]) # number=40
      prog.x(input_qubit[3]) # number=41
      prog.h(input_qubit[3]) # number=43
      prog.cz(input_qubit[0],input_qubit[3]) # number=44
      prog.h(input_qubit[3]) # number=45
      prog.h(input_qubit[3]) # number=30
      prog.cz(input_qubit[0],input_qubit[3]) # number=31
      prog.h(input_qubit[3]) # number=32
      prog.h(input_qubit[0]) # number=33
      prog.cz(input_qubit[3],input_qubit[0]) # number=34
      prog.rx(0.33300882128051834,input_qubit[2]) # number=36
      prog.h(input_qubit[0]) # number=35
      prog.cx(input_qubit[3],input_qubit[0]) # number=23
      prog.cx(input_qubit[3],input_qubit[0]) # number=46
      prog.z(input_qubit[3]) # number=47
      prog.cx(input_qubit[3],input_qubit[0]) # number=48
      prog.cx(input_qubit[3],input_qubit[0]) # number=25
      prog.cx(input_qubit[3],input_qubit[0]) # number=22
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[2]) # number=10
      prog.y(input_qubit[2]) # number=11
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = BasicAer.get_backend('statevector_simulator')
      sample_shot =8000
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_Class3343.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=5
  # total number=50
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      oracle = QuantumCircuit(controls, name="Zf")
  
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
  
              # oracle.h(controls[n])
              if n >= 2:
                  oracle.mcu1(pi, controls[1:], controls[0])
  
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
  
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[0]) # number=3
      prog.h(input_qubit[1]) # number=4
      prog.h(input_qubit[2]) # number=5
      prog.h(input_qubit[3]) # number=6
      prog.h(input_qubit[4])  # number=21
      prog.h(input_qubit[0]) # number=44
      prog.cz(input_qubit[3],input_qubit[0]) # number=45
      prog.h(input_qubit[0]) # number=46
      prog.cx(input_qubit[3],input_qubit[0]) # number=47
      prog.z(input_qubit[3]) # number=48
      prog.cx(input_qubit[3],input_qubit[0]) # number=49
      prog.cx(input_qubit[3],input_qubit[0]) # number=34
      prog.rx(0.11938052083641225,input_qubit[1]) # number=36
  
      Zf = build_oracle(n, f)
  
      repeat = floor(sqrt(2 ** n) * pi / 4)
      for i in range(repeat):
          prog.append(Zf.to_gate(), [input_qubit[i] for i in range(n)])
          prog.h(input_qubit[0])  # number=1
          prog.rx(1.4765485471872026,input_qubit[2]) # number=35
          prog.h(input_qubit[1])  # number=2
          prog.h(input_qubit[2])  # number=7
          prog.h(input_qubit[3])  # number=8
  
  
          prog.cx(input_qubit[1],input_qubit[0])  # number=41
          prog.x(input_qubit[0])  # number=42
          prog.cx(input_qubit[1],input_qubit[0])  # number=43
          prog.x(input_qubit[4]) # number=30
          prog.x(input_qubit[1])  # number=10
          prog.x(input_qubit[2])  # number=11
          prog.rx(0.45238934211692994,input_qubit[3]) # number=38
          prog.y(input_qubit[1]) # number=39
          prog.rx(-2.5258404934861938,input_qubit[1]) # number=25
          prog.h(input_qubit[3]) # number=29
          prog.cx(input_qubit[0],input_qubit[3])  # number=22
          prog.x(input_qubit[3])  # number=23
          prog.cx(input_qubit[0],input_qubit[3])  # number=24
  
          if n>=2:
              prog.mcu1(pi,input_qubit[1:],input_qubit[0])
  
          prog.x(input_qubit[0])  # number=13
          prog.rx(-0.0722566310325653,input_qubit[4]) # number=37
          prog.x(input_qubit[1])  # number=14
          prog.cx(input_qubit[0],input_qubit[2])  # number=26
          prog.x(input_qubit[2])  # number=27
          prog.h(input_qubit[4]) # number=40
          prog.cx(input_qubit[0],input_qubit[2])  # number=28
          prog.x(input_qubit[3])  # number=16
  
  
          prog.h(input_qubit[0])  # number=17
          prog.h(input_qubit[1])  # number=18
          prog.h(input_qubit[2])  # number=19
          prog.h(input_qubit[3])  # number=20
  
  
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  
  if __name__ == '__main__':
      key = "00000"
      f = lambda rep: str(int(rep == key))
      prog = make_circuit(5,f)
      backend = BasicAer.get_backend('qasm_simulator')
      sample_shot =7924
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit1590.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=18
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
      #for i in range(n):
      #    prog.measure(input_qubit[i], classicals[i])
  
      prog.y(input_qubit[1]) # number=2
      prog.y(input_qubit[1]) # number=4
      prog.y(input_qubit[1]) # number=3
      prog.rx(2.0860175219836226,input_qubit[1]) # number=7
      prog.x(input_qubit[0]) # number=5
      prog.x(input_qubit[0]) # number=6
      prog.h(input_qubit[0]) # number=10
      prog.cz(input_qubit[1],input_qubit[0]) # number=11
      prog.h(input_qubit[0]) # number=12
      prog.h(input_qubit[0]) # number=13
      prog.cz(input_qubit[1],input_qubit[0]) # number=14
      prog.h(input_qubit[0]) # number=15
      prog.x(input_qubit[1]) # number=16
      prog.x(input_qubit[1]) # number=17
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = BasicAer.get_backend('qasm_simulator')
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      circuit1.measure_all()
      prog = circuit1
  
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
  
      writefile = open("../data/startQiskit336.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=37
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=19
      prog.cz(input_qubit[0],input_qubit[3]) # number=20
      prog.h(input_qubit[3]) # number=21
      prog.h(input_qubit[3]) # number=24
      prog.cz(input_qubit[0],input_qubit[3]) # number=25
      prog.h(input_qubit[3]) # number=26
      prog.cx(input_qubit[0],input_qubit[3]) # number=31
      prog.x(input_qubit[3]) # number=32
      prog.h(input_qubit[3]) # number=34
      prog.cz(input_qubit[0],input_qubit[3]) # number=35
      prog.h(input_qubit[3]) # number=36
      prog.cx(input_qubit[0],input_qubit[3]) # number=18
      prog.cx(input_qubit[0],input_qubit[3]) # number=15
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.y(input_qubit[3]) # number=12
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.y(input_qubit[1]) # number=29
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[1]) # number=30
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[2]) # number=10
      prog.y(input_qubit[2]) # number=11
      prog.swap(input_qubit[3],input_qubit[0]) # number=22
      prog.swap(input_qubit[3],input_qubit[0]) # number=23
      prog.swap(input_qubit[1],input_qubit[0]) # number=27
      prog.swap(input_qubit[1],input_qubit[0]) # number=28
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = FakeVigo()
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_noisy2320.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=7
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  
  def build_oracle(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename=(kernel + '-oracle.png'))
      return oracle
  
  
  def build_circuit(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the Bernstein-Vazirani circuit
      zero = np.binary_repr(0, n)
      b = f(zero)
  
      # initial n + 1 bits
      input_qubit = QuantumRegister(n+1, "qc")
      classicals = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classicals)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(input_qubit[n])
      # circuit begin
      prog.h(input_qubit[1])  # number=1
      prog.x(input_qubit[2]) # number=2
      prog.cx(input_qubit[2],input_qubit[1]) # number=4
      prog.z(input_qubit[2]) # number=3
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
      prog.h(input_qubit[n])
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [input_qubit[n]])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      return prog
  
  
  def get_statevector(prog: QuantumCircuit) -> Any:
      state_backend = Aer.get_backend('statevector_simulator')
      statevec = execute(prog, state_backend).result()
      quantum_state = statevec.get_statevector()
      qubits = round(log2(len(quantum_state)))
      quantum_state = {
          "|" + np.binary_repr(i, qubits) + ">": quantum_state[i]
          for i in range(2 ** qubits)
      }
      return quantum_state
  
  
  def evaluate(backend_str: str, prog: QuantumCircuit, shots: int, b: str) -> Any:
      # Q: which backend should we use?
  
      # get state vector
      quantum_state = get_statevector(prog)
  
      # get simulate results
  
      # provider = IBMQ.load_account()
      # backend = provider.get_backend(backend_str)
      # qobj = compile(prog, backend, shots)
      # job = backend.run(qobj)
      # job.result()
      backend = Aer.get_backend(backend_str)
      # transpile/schedule -> assemble -> backend.run
      results = execute(prog, backend, shots=shots).result()
      counts = results.get_counts()
      a = Counter(counts).most_common(1)[0][0][::-1]
  
      return {
          "measurements": counts,
          # "state": statevec,
          "quantum_state": quantum_state,
          "a": a,
          "b": b
      }
  
  
  def bernstein_test_1(rep: str):
      """011 . x + 1"""
      a = "011"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_2(rep: str):
      """000 . x + 0"""
      a = "000"
      b = "0"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_3(rep: str):
      """111 . x + 1"""
      a = "111"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  if __name__ == "__main__":
      n = 2
      a = "11"
      b = "1"
      f = lambda rep: \
          bitwise_xor(bitwise_dot(a, rep), b)
      prog = build_circuit(n, f)
      sample_shot =4000
      writefile = open("../data/startQiskit_noisy43.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      backend = FakeYorktown()
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.h(qubit=2)
      circuit1.x(qubit=3)
      circuit1.measure_all()
  
      info = execute(circuit1,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=3
  # total number=12
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  import networkx as nx
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def make_circuit(n:int) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      prog = QuantumCircuit(input_qubit)
      prog.h(input_qubit[0]) # number=1
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2])  # number=3
      prog.h(input_qubit[3])  # number=4
  
      for edge in E:
          k = edge[0]
          l = edge[1]
          prog.cp(-2 * gamma, input_qubit[k-1], input_qubit[l-1])
          prog.p(gamma, k)
          prog.p(gamma, l)
  
      prog.rx(2 * beta, range(len(V)))
  
      prog.x(input_qubit[2]) # number=5
      prog.cx(input_qubit[0],input_qubit[2]) # number=9
      prog.x(input_qubit[2]) # number=10
      prog.cx(input_qubit[0],input_qubit[2]) # number=11
      prog.cx(input_qubit[1],input_qubit[0]) # number=7
      prog.cx(input_qubit[1],input_qubit[0]) # number=8
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      n = 4
      V = np.arange(0, n, 1)
      E = [(0, 1, 1.0), (0, 2, 1.0), (1, 2, 1.0), (3, 2, 1.0), (3, 1, 1.0)]
  
      G = nx.Graph()
      G.add_nodes_from(V)
      G.add_weighted_edges_from(E)
  
      step_size = 0.1
  
      a_gamma = np.arange(0, np.pi, step_size)
      a_beta = np.arange(0, np.pi, step_size)
      a_gamma, a_beta = np.meshgrid(a_gamma, a_beta)
  
      F1 = 3 - (np.sin(2 * a_beta) ** 2 * np.sin(2 * a_gamma) ** 2 - 0.5 * np.sin(4 * a_beta) * np.sin(4 * a_gamma)) * (
                  1 + np.cos(4 * a_gamma) ** 2)
  
      result = np.where(F1 == np.amax(F1))
      a = list(zip(result[0], result[1]))[0]
  
      gamma = a[0] * step_size
      beta = a[1] * step_size
  
      prog = make_circuit(4)
      sample_shot =3962
      writefile = open("../data/startQiskit_QC77.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = provider.get_backend("ibmq_5_yorktown")
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.measure_all()
      prog = circuit1
  
      info = execute(prog,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=5
  # total number=42
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      oracle = QuantumCircuit(controls, name="Zf")
  
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
  
              # oracle.h(controls[n])
              if n >= 2:
                  oracle.mcu1(pi, controls[1:], controls[0])
  
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
  
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[0]) # number=3
      prog.h(input_qubit[1]) # number=4
      prog.h(input_qubit[2]) # number=5
      prog.h(input_qubit[3]) # number=6
      prog.h(input_qubit[4])  # number=21
      prog.h(input_qubit[0]) # number=39
      prog.cz(input_qubit[3],input_qubit[0]) # number=40
      prog.h(input_qubit[0]) # number=41
      prog.z(input_qubit[3]) # number=33
      prog.cx(input_qubit[3],input_qubit[0]) # number=34
      prog.rx(0.11938052083641225,input_qubit[1]) # number=36
  
      Zf = build_oracle(n, f)
  
      repeat = floor(sqrt(2 ** n) * pi / 4)
      for i in range(repeat):
          prog.append(Zf.to_gate(), [input_qubit[i] for i in range(n)])
          prog.h(input_qubit[0])  # number=1
          prog.rx(1.4765485471872026,input_qubit[2]) # number=35
          prog.h(input_qubit[1])  # number=2
          prog.h(input_qubit[2])  # number=7
          prog.h(input_qubit[3])  # number=8
  
  
          prog.x(input_qubit[0])  # number=9
          prog.x(input_qubit[4]) # number=30
          prog.x(input_qubit[1])  # number=10
          prog.x(input_qubit[2])  # number=11
          prog.rx(0.45238934211692994,input_qubit[3]) # number=38
          prog.rx(-2.5258404934861938,input_qubit[1]) # number=25
          prog.h(input_qubit[3]) # number=29
          prog.cx(input_qubit[0],input_qubit[3])  # number=22
          prog.x(input_qubit[3])  # number=23
          prog.cx(input_qubit[0],input_qubit[3])  # number=24
  
          if n>=2:
              prog.mcu1(pi,input_qubit[1:],input_qubit[0])
  
          prog.x(input_qubit[0])  # number=13
          prog.rx(-0.0722566310325653,input_qubit[4]) # number=37
          prog.x(input_qubit[1])  # number=14
          prog.cx(input_qubit[0],input_qubit[2])  # number=26
          prog.x(input_qubit[2])  # number=27
          prog.cx(input_qubit[0],input_qubit[2])  # number=28
          prog.x(input_qubit[3])  # number=16
  
  
          prog.h(input_qubit[0])  # number=17
          prog.h(input_qubit[1])  # number=18
          prog.h(input_qubit[2])  # number=19
          prog.h(input_qubit[3])  # number=20
  
  
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  
  if __name__ == '__main__':
      key = "00000"
      f = lambda rep: str(int(rep == key))
      prog = make_circuit(5,f)
      backend = BasicAer.get_backend('qasm_simulator')
      sample_shot =7924
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit1126.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=41
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=35
      prog.cz(input_qubit[0],input_qubit[3]) # number=36
      prog.h(input_qubit[3]) # number=37
      prog.h(input_qubit[3]) # number=22
      prog.cx(input_qubit[0],input_qubit[3]) # number=32
      prog.x(input_qubit[3]) # number=33
      prog.cx(input_qubit[0],input_qubit[3]) # number=34
      prog.h(input_qubit[3]) # number=19
      prog.cz(input_qubit[0],input_qubit[3]) # number=20
      prog.h(input_qubit[3]) # number=21
      prog.cx(input_qubit[3],input_qubit[0]) # number=38
      prog.z(input_qubit[3]) # number=39
      prog.cx(input_qubit[3],input_qubit[0]) # number=40
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
      prog.h(input_qubit[0]) # number=26
      prog.cz(input_qubit[1],input_qubit[0]) # number=27
      prog.h(input_qubit[0]) # number=28
      prog.z(input_qubit[1]) # number=24
      prog.h(input_qubit[0]) # number=29
      prog.cz(input_qubit[1],input_qubit[0]) # number=30
      prog.h(input_qubit[0]) # number=31
      prog.h(input_qubit[1]) # number=18
      prog.rx(2.8902652413026093,input_qubit[2]) # number=13
  
      prog.y(input_qubit[1]) # number=11
      prog.y(input_qubit[1]) # number=12
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = FakeVigo()
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_noisy2660.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=11
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  import networkx as nx
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def make_circuit(n:int) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      prog = QuantumCircuit(input_qubit)
      prog.h(input_qubit[0]) # number=1
      prog.cx(input_qubit[3],input_qubit[0]) # number=8
      prog.z(input_qubit[3]) # number=9
      prog.cx(input_qubit[3],input_qubit[0]) # number=10
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2])  # number=3
      prog.h(input_qubit[3])  # number=4
  
      for edge in E:
          k = edge[0]
          l = edge[1]
          prog.cp(-2 * gamma, input_qubit[k-1], input_qubit[l-1])
          prog.p(gamma, k)
          prog.p(gamma, l)
  
      prog.rx(2 * beta, range(len(V)))
  
      prog.swap(input_qubit[3],input_qubit[0]) # number=5
      prog.swap(input_qubit[3],input_qubit[0]) # number=6
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      n = 4
      V = np.arange(0, n, 1)
      E = [(0, 1, 1.0), (0, 2, 1.0), (1, 2, 1.0), (3, 2, 1.0), (3, 1, 1.0)]
  
      G = nx.Graph()
      G.add_nodes_from(V)
      G.add_weighted_edges_from(E)
  
      step_size = 0.1
  
      a_gamma = np.arange(0, np.pi, step_size)
      a_beta = np.arange(0, np.pi, step_size)
      a_gamma, a_beta = np.meshgrid(a_gamma, a_beta)
  
      F1 = 3 - (np.sin(2 * a_beta) ** 2 * np.sin(2 * a_gamma) ** 2 - 0.5 * np.sin(4 * a_beta) * np.sin(4 * a_gamma)) * (
                  1 + np.cos(4 * a_gamma) ** 2)
  
      result = np.where(F1 == np.amax(F1))
      a = list(zip(result[0], result[1]))[0]
  
      gamma = a[0] * step_size
      beta = a[1] * step_size
  
      prog = make_circuit(4)
      sample_shot =5600
      writefile = open("../data/startQiskit92.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      backend = BasicAer.get_backend('qasm_simulator')
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.measure_all()
      prog = circuit1
  
      info = execute(prog,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=3
  # total number=73
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  
  def build_oracle(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename=(kernel + '-oracle.png'))
      return oracle
  
  
  def build_circuit(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the Bernstein-Vazirani circuit
      zero = np.binary_repr(0, n)
      b = f(zero)
  
      # initial n + 1 bits
      input_qubit = QuantumRegister(n+1, "qc")
      classicals = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classicals)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(input_qubit[n])
      # circuit begin
      prog.h(input_qubit[1])  # number=1
      prog.h(input_qubit[2]) # number=38
      prog.cz(input_qubit[0],input_qubit[2]) # number=39
      prog.h(input_qubit[2]) # number=40
      prog.h(input_qubit[2]) # number=59
      prog.cz(input_qubit[0],input_qubit[2]) # number=60
      prog.h(input_qubit[2]) # number=61
      prog.h(input_qubit[2]) # number=42
      prog.cz(input_qubit[0],input_qubit[2]) # number=43
      prog.h(input_qubit[2]) # number=44
      prog.h(input_qubit[2]) # number=48
      prog.cz(input_qubit[0],input_qubit[2]) # number=49
      prog.h(input_qubit[2]) # number=50
      prog.h(input_qubit[2]) # number=70
      prog.cz(input_qubit[0],input_qubit[2]) # number=71
      prog.h(input_qubit[2]) # number=72
      prog.x(input_qubit[2]) # number=55
      prog.h(input_qubit[2]) # number=67
      prog.cz(input_qubit[0],input_qubit[2]) # number=68
      prog.h(input_qubit[2]) # number=69
      prog.h(input_qubit[2]) # number=64
      prog.cz(input_qubit[0],input_qubit[2]) # number=65
      prog.h(input_qubit[2]) # number=66
      prog.cx(input_qubit[0],input_qubit[2]) # number=37
      prog.h(input_qubit[2]) # number=51
      prog.cz(input_qubit[0],input_qubit[2]) # number=52
      prog.h(input_qubit[2]) # number=53
      prog.h(input_qubit[2]) # number=25
      prog.cz(input_qubit[0],input_qubit[2]) # number=26
      prog.h(input_qubit[2]) # number=27
      prog.h(input_qubit[1]) # number=7
      prog.cz(input_qubit[2],input_qubit[1]) # number=8
      prog.rx(0.17592918860102857,input_qubit[2]) # number=34
      prog.rx(-0.3989822670059037,input_qubit[1]) # number=30
      prog.h(input_qubit[1]) # number=9
      prog.h(input_qubit[1]) # number=18
      prog.rx(2.3310617489636263,input_qubit[2]) # number=58
      prog.cz(input_qubit[2],input_qubit[1]) # number=19
      prog.h(input_qubit[1]) # number=20
      prog.x(input_qubit[1]) # number=62
      prog.y(input_qubit[1]) # number=14
      prog.h(input_qubit[1]) # number=22
      prog.cz(input_qubit[2],input_qubit[1]) # number=23
      prog.rx(-0.9173450548482197,input_qubit[1]) # number=57
      prog.cx(input_qubit[2],input_qubit[1]) # number=63
      prog.h(input_qubit[1]) # number=24
      prog.z(input_qubit[2]) # number=3
      prog.z(input_qubit[1]) # number=41
      prog.x(input_qubit[1]) # number=17
      prog.y(input_qubit[2]) # number=5
      prog.x(input_qubit[2]) # number=21
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
      prog.h(input_qubit[n])
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [input_qubit[n]])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      return prog
  
  
  def get_statevector(prog: QuantumCircuit) -> Any:
      state_backend = Aer.get_backend('statevector_simulator')
      statevec = execute(prog, state_backend).result()
      quantum_state = statevec.get_statevector()
      qubits = round(log2(len(quantum_state)))
      quantum_state = {
          "|" + np.binary_repr(i, qubits) + ">": quantum_state[i]
          for i in range(2 ** qubits)
      }
      return quantum_state
  
  
  def evaluate(backend_str: str, prog: QuantumCircuit, shots: int, b: str) -> Any:
      # Q: which backend should we use?
  
      # get state vector
      quantum_state = get_statevector(prog)
  
      # get simulate results
  
      # provider = IBMQ.load_account()
      # backend = provider.get_backend(backend_str)
      # qobj = compile(prog, backend, shots)
      # job = backend.run(qobj)
      # job.result()
      backend = Aer.get_backend(backend_str)
      # transpile/schedule -> assemble -> backend.run
      results = execute(prog, backend, shots=shots).result()
      counts = results.get_counts()
      a = Counter(counts).most_common(1)[0][0][::-1]
  
      return {
          "measurements": counts,
          # "state": statevec,
          "quantum_state": quantum_state,
          "a": a,
          "b": b
      }
  
  
  def bernstein_test_1(rep: str):
      """011 . x + 1"""
      a = "011"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_2(rep: str):
      """000 . x + 0"""
      a = "000"
      b = "0"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_3(rep: str):
      """111 . x + 1"""
      a = "111"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  if __name__ == "__main__":
      n = 2
      a = "11"
      b = "1"
      f = lambda rep: \
          bitwise_xor(bitwise_dot(a, rep), b)
      prog = build_circuit(n, f)
      sample_shot =4000
      writefile = open("../data/startQiskit_Class364.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      backend = BasicAer.get_backend('statevector_simulator')
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.h(qubit=2)
      circuit1.x(qubit=3)
  
      info = execute(circuit1,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=2
  # total number=10
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.h(input_qubit[1]) # number=4
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      prog.cx(input_qubit[0],input_qubit[1]) # number=7
      prog.x(input_qubit[1]) # number=8
      prog.cx(input_qubit[0],input_qubit[1]) # number=9
      prog.x(input_qubit[1]) # number=3
      prog.cx(input_qubit[1],input_qubit[0]) # number=5
      prog.cx(input_qubit[1],input_qubit[0]) # number=6
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = BasicAer.get_backend('statevector_simulator')
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      prog = circuit1
  
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
  
      writefile = open("../data/startQiskit_Class143.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=46
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=20
      prog.cz(input_qubit[0],input_qubit[3]) # number=21
      prog.h(input_qubit[3]) # number=22
      prog.x(input_qubit[3]) # number=13
      prog.h(input_qubit[3]) # number=23
      prog.cz(input_qubit[0],input_qubit[3]) # number=24
      prog.h(input_qubit[3]) # number=25
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.h(input_qubit[0]) # number=5
      prog.y(input_qubit[2]) # number=18
      prog.h(input_qubit[0]) # number=43
      prog.cz(input_qubit[3],input_qubit[0]) # number=44
      prog.h(input_qubit[0]) # number=45
      prog.z(input_qubit[3]) # number=41
      prog.cx(input_qubit[3],input_qubit[0]) # number=42
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.h(input_qubit[0]) # number=33
      prog.cz(input_qubit[2],input_qubit[0]) # number=34
      prog.h(input_qubit[0]) # number=35
      prog.h(input_qubit[1]) # number=19
      prog.h(input_qubit[0]) # number=15
      prog.cz(input_qubit[2],input_qubit[0]) # number=16
      prog.h(input_qubit[0]) # number=17
      prog.rx(1.6838936623241292,input_qubit[2]) # number=36
      prog.y(input_qubit[1]) # number=26
      prog.y(input_qubit[1]) # number=27
      prog.swap(input_qubit[1],input_qubit[0]) # number=29
      prog.swap(input_qubit[1],input_qubit[0]) # number=30
      prog.x(input_qubit[0]) # number=31
      prog.cx(input_qubit[1],input_qubit[0]) # number=37
      prog.x(input_qubit[0]) # number=38
      prog.cx(input_qubit[1],input_qubit[0]) # number=39
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = BasicAer.get_backend('statevector_simulator')
      sample_shot =8000
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_Class3399.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=49
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.cx(input_qubit[0],input_qubit[3]) # number=13
      prog.cx(input_qubit[0],input_qubit[3]) # number=17
      prog.x(input_qubit[3]) # number=18
      prog.cx(input_qubit[0],input_qubit[3]) # number=19
      prog.cx(input_qubit[0],input_qubit[3]) # number=15
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.y(input_qubit[3]) # number=12
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3]) # number=37
      prog.cz(input_qubit[0],input_qubit[3]) # number=38
      prog.h(input_qubit[3]) # number=39
      prog.cx(input_qubit[0],input_qubit[3]) # number=40
      prog.cx(input_qubit[0],input_qubit[3]) # number=46
      prog.x(input_qubit[3]) # number=47
      prog.cx(input_qubit[0],input_qubit[3]) # number=48
      prog.h(input_qubit[3]) # number=43
      prog.cz(input_qubit[0],input_qubit[3]) # number=44
      prog.h(input_qubit[3]) # number=45
      prog.h(input_qubit[3]) # number=30
      prog.cz(input_qubit[0],input_qubit[3]) # number=31
      prog.h(input_qubit[3]) # number=32
      prog.h(input_qubit[0]) # number=33
      prog.cz(input_qubit[3],input_qubit[0]) # number=34
      prog.rx(0.33300882128051834,input_qubit[2]) # number=36
      prog.h(input_qubit[0]) # number=35
      prog.cx(input_qubit[3],input_qubit[0]) # number=23
      prog.z(input_qubit[3]) # number=24
      prog.cx(input_qubit[3],input_qubit[0]) # number=25
      prog.cx(input_qubit[3],input_qubit[0]) # number=22
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[2]) # number=10
      prog.y(input_qubit[2]) # number=11
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = FakeVigo()
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_noisy3345.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=13
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.h(input_qubit[1]) # number=8
      prog.cz(input_qubit[0],input_qubit[1]) # number=9
      prog.h(input_qubit[1]) # number=10
      prog.cx(input_qubit[0],input_qubit[1]) # number=5
      prog.cx(input_qubit[0],input_qubit[1]) # number=7
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
      #for i in range(n):
      #    prog.measure(input_qubit[i], classicals[i])
  
      prog.x(input_qubit[0]) # number=3
      prog.y(input_qubit[1]) # number=6
      prog.x(input_qubit[0]) # number=4
      prog.x(input_qubit[1]) # number=11
      prog.x(input_qubit[1]) # number=12
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = BasicAer.get_backend('qasm_simulator')
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      circuit1.measure_all()
      prog = circuit1
  
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
  
      writefile = open("../data/startQiskit250.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=5
  # total number=69
  import cirq
  import qiskit
  from qiskit import IBMQ
  from qiskit.providers.ibmq import least_busy
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      oracle = QuantumCircuit(controls, name="Zf")
  
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
  
              # oracle.h(controls[n])
              if n >= 2:
                  oracle.mcu1(pi, controls[1:], controls[0])
  
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
  
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[0]) # number=3
      prog.h(input_qubit[1]) # number=4
      prog.h(input_qubit[0]) # number=57
      prog.cz(input_qubit[4],input_qubit[0]) # number=58
      prog.h(input_qubit[0]) # number=59
      prog.z(input_qubit[4]) # number=55
      prog.cx(input_qubit[4],input_qubit[0]) # number=56
      prog.h(input_qubit[2]) # number=50
      prog.cz(input_qubit[4],input_qubit[2]) # number=51
      prog.h(input_qubit[2]) # number=52
      prog.h(input_qubit[2]) # number=5
      prog.h(input_qubit[3]) # number=6
      prog.h(input_qubit[4])  # number=21
  
      Zf = build_oracle(n, f)
  
      repeat = floor(sqrt(2 ** n) * pi / 4)
      for i in range(repeat):
          prog.append(Zf.to_gate(), [input_qubit[i] for i in range(n)])
          prog.h(input_qubit[0])  # number=1
          prog.h(input_qubit[1])  # number=2
          prog.h(input_qubit[2])  # number=7
          prog.h(input_qubit[3])  # number=8
  
  
          prog.h(input_qubit[0])  # number=28
          prog.h(input_qubit[0]) # number=66
          prog.cz(input_qubit[3],input_qubit[0]) # number=67
          prog.h(input_qubit[0]) # number=68
          prog.z(input_qubit[3]) # number=61
          prog.cx(input_qubit[3],input_qubit[0]) # number=62
          prog.cz(input_qubit[1],input_qubit[0])  # number=29
          prog.h(input_qubit[0])  # number=30
          prog.h(input_qubit[0])  # number=43
          prog.cz(input_qubit[1],input_qubit[0])  # number=44
          prog.h(input_qubit[0])  # number=45
          prog.cx(input_qubit[1],input_qubit[0])  # number=35
          prog.cx(input_qubit[1],input_qubit[0])  # number=38
          prog.x(input_qubit[0])  # number=39
          prog.cx(input_qubit[1],input_qubit[0])  # number=40
          prog.cx(input_qubit[1],input_qubit[0])  # number=37
          prog.h(input_qubit[0])  # number=46
          prog.cz(input_qubit[1],input_qubit[0])  # number=47
          prog.h(input_qubit[0])  # number=48
          prog.h(input_qubit[0])  # number=63
          prog.cz(input_qubit[1],input_qubit[0])  # number=64
          prog.h(input_qubit[0])  # number=65
          prog.x(input_qubit[1])  # number=10
          prog.x(input_qubit[2])  # number=11
          prog.x(input_qubit[3])  # number=12
  
          if n>=2:
              prog.mcu1(pi,input_qubit[1:],input_qubit[0])
  
          prog.x(input_qubit[0])  # number=13
          prog.cx(input_qubit[0],input_qubit[1])  # number=22
          prog.y(input_qubit[2]) # number=41
          prog.x(input_qubit[1])  # number=23
          prog.cx(input_qubit[0],input_qubit[1])  # number=24
          prog.rx(1.0398671683382215,input_qubit[2]) # number=31
          prog.x(input_qubit[2])  # number=15
          prog.x(input_qubit[3])  # number=16
  
  
          prog.h(input_qubit[0])  # number=17
          prog.h(input_qubit[1])  # number=18
          prog.h(input_qubit[2])  # number=19
          prog.h(input_qubit[3])  # number=20
  
  
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  
  if __name__ == '__main__':
      key = "00000"
      f = lambda rep: str(int(rep == key))
      prog = make_circuit(5,f)
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2 and not x.configuration().simulator and x.status().operational == True))
      sample_shot =7924
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_QC1883.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=47
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=16
      prog.cz(input_qubit[0],input_qubit[3]) # number=17
      prog.rx(-0.5686282702997527,input_qubit[3]) # number=32
      prog.h(input_qubit[3]) # number=18
      prog.h(input_qubit[3]) # number=26
      prog.cz(input_qubit[0],input_qubit[3]) # number=27
      prog.h(input_qubit[3]) # number=28
      prog.x(input_qubit[3]) # number=21
      prog.rx(0.4241150082346221,input_qubit[2]) # number=33
      prog.cx(input_qubit[0],input_qubit[3]) # number=22
      prog.cx(input_qubit[0],input_qubit[3]) # number=12
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.rx(-0.9927432785343745,input_qubit[1]) # number=43
      prog.h(input_qubit[2]) # number=23
      prog.cz(input_qubit[1],input_qubit[2]) # number=24
      prog.h(input_qubit[2]) # number=25
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0]) # number=34
      prog.cz(input_qubit[2],input_qubit[0]) # number=35
      prog.h(input_qubit[0]) # number=36
      prog.cx(input_qubit[2],input_qubit[0]) # number=37
      prog.cx(input_qubit[2],input_qubit[0]) # number=44
      prog.z(input_qubit[2]) # number=45
      prog.cx(input_qubit[2],input_qubit[0]) # number=46
      prog.cx(input_qubit[2],input_qubit[0]) # number=39
      prog.h(input_qubit[0]) # number=40
      prog.cz(input_qubit[2],input_qubit[0]) # number=41
      prog.h(input_qubit[0]) # number=42
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[0]) # number=14
      prog.y(input_qubit[0]) # number=15
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = FakeVigo()
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_noisy3210.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=73
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  
  def build_oracle(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename=(kernel + '-oracle.png'))
      return oracle
  
  
  def build_circuit(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the Bernstein-Vazirani circuit
      zero = np.binary_repr(0, n)
      b = f(zero)
  
      # initial n + 1 bits
      input_qubit = QuantumRegister(n+1, "qc")
      classicals = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classicals)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(input_qubit[n])
      # circuit begin
      prog.h(input_qubit[1])  # number=1
      prog.rx(-0.09738937226128368,input_qubit[2]) # number=2
      prog.h(input_qubit[1]) # number=33
      prog.y(input_qubit[2]) # number=56
      prog.cz(input_qubit[2],input_qubit[1]) # number=34
      prog.h(input_qubit[1]) # number=35
      prog.h(input_qubit[1]) # number=3
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
      prog.h(input_qubit[n])
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [input_qubit[n]])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      return prog
  
  
  def get_statevector(prog: QuantumCircuit) -> Any:
      state_backend = Aer.get_backend('statevector_simulator')
      statevec = execute(prog, state_backend).result()
      quantum_state = statevec.get_statevector()
      qubits = round(log2(len(quantum_state)))
      quantum_state = {
          "|" + np.binary_repr(i, qubits) + ">": quantum_state[i]
          for i in range(2 ** qubits)
      }
      return quantum_state
  
  
  def evaluate(backend_str: str, prog: QuantumCircuit, shots: int, b: str) -> Any:
      # Q: which backend should we use?
  
      # get state vector
      quantum_state = get_statevector(prog)
  
      # get simulate results
  
      # provider = IBMQ.load_account()
      # backend = provider.get_backend(backend_str)
      # qobj = compile(prog, backend, shots)
      # job = backend.run(qobj)
      # job.result()
      backend = Aer.get_backend(backend_str)
      # transpile/schedule -> assemble -> backend.run
      results = execute(prog, backend, shots=shots).result()
      counts = results.get_counts()
      a = Counter(counts).most_common(1)[0][0][::-1]
  
      return {
          "measurements": counts,
          # "state": statevec,
          "quantum_state": quantum_state,
          "a": a,
          "b": b
      }
  
  
  def bernstein_test_1(rep: str):
      """011 . x + 1"""
      a = "011"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_2(rep: str):
      """000 . x + 0"""
      a = "000"
      b = "0"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_3(rep: str):
      """111 . x + 1"""
      a = "111"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  if __name__ == "__main__":
      n = 2
      a = "11"
      b = "1"
      f = lambda rep: \
          bitwise_xor(bitwise_dot(a, rep), b)
      prog = build_circuit(n, f)
      sample_shot =4000
      writefile = open("../data/startQiskit_Class408.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      backend = BasicAer.get_backend('statevector_simulator')
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.h(qubit=2)
      circuit1.x(qubit=3)
  
      info = execute(circuit1,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=5
  # total number=54
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      oracle = QuantumCircuit(controls, name="Zf")
  
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
  
              # oracle.h(controls[n])
              if n >= 2:
                  oracle.mcu1(pi, controls[1:], controls[0])
  
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
  
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[0]) # number=3
      prog.h(input_qubit[1]) # number=4
      prog.h(input_qubit[2]) # number=5
      prog.h(input_qubit[3]) # number=6
      prog.h(input_qubit[4])  # number=21
  
      Zf = build_oracle(n, f)
  
      repeat = floor(sqrt(2 ** n) * pi / 4)
      for i in range(repeat):
          prog.append(Zf.to_gate(), [input_qubit[i] for i in range(n)])
          prog.h(input_qubit[0])  # number=1
          prog.h(input_qubit[1])  # number=2
          prog.h(input_qubit[2])  # number=7
          prog.h(input_qubit[3])  # number=8
  
  
          prog.h(input_qubit[0])  # number=31
          prog.cz(input_qubit[1],input_qubit[0])  # number=32
          prog.h(input_qubit[0])  # number=33
          prog.h(input_qubit[1]) # number=44
          prog.cz(input_qubit[0],input_qubit[1]) # number=45
          prog.h(input_qubit[1]) # number=46
          prog.x(input_qubit[1]) # number=41
          prog.h(input_qubit[1]) # number=48
          prog.cz(input_qubit[0],input_qubit[1]) # number=49
          prog.h(input_qubit[1]) # number=50
          prog.cx(input_qubit[1],input_qubit[0])  # number=51
          prog.x(input_qubit[0])  # number=52
          prog.cx(input_qubit[1],input_qubit[0])  # number=53
          prog.cx(input_qubit[1],input_qubit[0])  # number=27
          prog.h(input_qubit[1])  # number=37
          prog.cz(input_qubit[0],input_qubit[1])  # number=38
          prog.h(input_qubit[1])  # number=39
          prog.x(input_qubit[1])  # number=35
          prog.cx(input_qubit[0],input_qubit[1])  # number=36
          prog.x(input_qubit[2])  # number=11
          prog.x(input_qubit[3])  # number=12
          prog.cx(input_qubit[3],input_qubit[2]) # number=43
          prog.cx(input_qubit[3],input_qubit[2]) # number=47
  
          if n>=2:
              prog.mcu1(pi,input_qubit[1:],input_qubit[0])
  
          prog.x(input_qubit[0])  # number=13
          prog.cx(input_qubit[0],input_qubit[1])  # number=22
          prog.x(input_qubit[1])  # number=23
          prog.cx(input_qubit[0],input_qubit[1])  # number=24
          prog.x(input_qubit[2])  # number=15
          prog.x(input_qubit[1]) # number=29
          prog.y(input_qubit[4]) # number=28
          prog.x(input_qubit[3])  # number=16
  
  
          prog.h(input_qubit[0])  # number=17
          prog.h(input_qubit[1])  # number=18
          prog.h(input_qubit[2])  # number=19
          prog.h(input_qubit[3])  # number=20
  
  
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  
  if __name__ == '__main__':
      key = "00000"
      f = lambda rep: str(int(rep == key))
      prog = make_circuit(5,f)
      backend = FakeVigo()
      sample_shot =7924
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_noisy1314.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=13
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  import networkx as nx
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def make_circuit(n:int) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      prog = QuantumCircuit(input_qubit)
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2])  # number=3
      prog.h(input_qubit[3])  # number=4
  
      for edge in E:
          k = edge[0]
          l = edge[1]
          prog.cp(-2 * gamma, input_qubit[k-1], input_qubit[l-1])
          prog.p(gamma, k)
          prog.p(gamma, l)
  
      prog.rx(2 * beta, range(len(V)))
  
      prog.swap(input_qubit[1],input_qubit[0]) # number=6
      prog.swap(input_qubit[1],input_qubit[0]) # number=7
      prog.cx(input_qubit[0],input_qubit[1]) # number=10
      prog.x(input_qubit[1]) # number=11
      prog.cx(input_qubit[0],input_qubit[1]) # number=12
      prog.x(input_qubit[1]) # number=9
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      n = 4
      V = np.arange(0, n, 1)
      E = [(0, 1, 1.0), (0, 2, 1.0), (1, 2, 1.0), (3, 2, 1.0), (3, 1, 1.0)]
  
      G = nx.Graph()
      G.add_nodes_from(V)
      G.add_weighted_edges_from(E)
  
      step_size = 0.1
  
      a_gamma = np.arange(0, np.pi, step_size)
      a_beta = np.arange(0, np.pi, step_size)
      a_gamma, a_beta = np.meshgrid(a_gamma, a_beta)
  
      F1 = 3 - (np.sin(2 * a_beta) ** 2 * np.sin(2 * a_gamma) ** 2 - 0.5 * np.sin(4 * a_beta) * np.sin(4 * a_gamma)) * (
                  1 + np.cos(4 * a_gamma) ** 2)
  
      result = np.where(F1 == np.amax(F1))
      a = list(zip(result[0], result[1]))[0]
  
      gamma = a[0] * step_size
      beta = a[1] * step_size
  
      prog = make_circuit(4)
      sample_shot =5600
      writefile = open("../data/startQiskit_QC575.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = provider.get_backend("ibmq_5_yorktown")
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.measure_all()
      prog = circuit1
  
      info = execute(prog,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=5
  # total number=55
  import cirq
  import qiskit
  from qiskit import IBMQ
  from qiskit.providers.ibmq import least_busy
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      oracle = QuantumCircuit(controls, name="Zf")
  
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
  
              # oracle.h(controls[n])
              if n >= 2:
                  oracle.mcu1(pi, controls[1:], controls[0])
  
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
  
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[0]) # number=3
      prog.h(input_qubit[1]) # number=4
      prog.cx(input_qubit[0],input_qubit[1]) # number=52
      prog.x(input_qubit[1]) # number=53
      prog.cx(input_qubit[0],input_qubit[1]) # number=54
      prog.h(input_qubit[1]) # number=26
      prog.cz(input_qubit[4],input_qubit[1]) # number=27
      prog.h(input_qubit[1]) # number=28
      prog.h(input_qubit[2]) # number=5
      prog.h(input_qubit[3]) # number=6
      prog.h(input_qubit[4])  # number=21
      prog.h(input_qubit[1]) # number=34
      prog.cz(input_qubit[4],input_qubit[1]) # number=35
      prog.z(input_qubit[4]) # number=46
      prog.rx(0.8011061266653969,input_qubit[2]) # number=37
      prog.h(input_qubit[1]) # number=36
      prog.z(input_qubit[3]) # number=51
  
      Zf = build_oracle(n, f)
  
      repeat = floor(sqrt(2 ** n) * pi / 4)
      for i in range(repeat):
          prog.append(Zf.to_gate(), [input_qubit[i] for i in range(n)])
          prog.h(input_qubit[0])  # number=1
          prog.h(input_qubit[1])  # number=2
          prog.h(input_qubit[2])  # number=7
          prog.h(input_qubit[3])  # number=8
  
  
          prog.cx(input_qubit[1],input_qubit[0])  # number=38
          prog.x(input_qubit[0])  # number=39
          prog.cx(input_qubit[1],input_qubit[0])  # number=40
          prog.cx(input_qubit[0],input_qubit[1])  # number=42
          prog.rx(-1.928937889304133,input_qubit[2]) # number=49
          prog.x(input_qubit[1])  # number=43
          prog.cx(input_qubit[0],input_qubit[1])  # number=44
          prog.x(input_qubit[2])  # number=11
          prog.y(input_qubit[1]) # number=45
          prog.x(input_qubit[3])  # number=12
          prog.h(input_qubit[2]) # number=41
  
          if n>=2:
              prog.mcu1(pi,input_qubit[1:],input_qubit[0])
  
          prog.cx(input_qubit[1],input_qubit[0])  # number=22
          prog.x(input_qubit[4]) # number=47
          prog.x(input_qubit[0])  # number=23
          prog.cx(input_qubit[1],input_qubit[0])  # number=24
          prog.cx(input_qubit[0],input_qubit[1])  # number=30
          prog.x(input_qubit[1])  # number=31
          prog.cx(input_qubit[0],input_qubit[1])  # number=32
          prog.x(input_qubit[2])  # number=15
          prog.h(input_qubit[4]) # number=29
          prog.x(input_qubit[3])  # number=16
          prog.z(input_qubit[3]) # number=50
  
  
          prog.h(input_qubit[0])  # number=17
          prog.h(input_qubit[1])  # number=18
          prog.h(input_qubit[2])  # number=19
          prog.h(input_qubit[3])  # number=20
  
  
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  
  if __name__ == '__main__':
      key = "00000"
      f = lambda rep: str(int(rep == key))
      prog = make_circuit(5,f)
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2 and not x.configuration().simulator and x.status().operational == True))
      sample_shot =7924
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_QC1911.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=16
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      prog.y(input_qubit[1]) # number=2
      prog.y(input_qubit[1]) # number=4
      prog.y(input_qubit[1]) # number=3
      prog.h(input_qubit[0]) # number=13
      prog.cz(input_qubit[1],input_qubit[0]) # number=14
      prog.h(input_qubit[0]) # number=15
      prog.x(input_qubit[0]) # number=8
      prog.cx(input_qubit[1],input_qubit[0]) # number=9
      prog.cx(input_qubit[1],input_qubit[0]) # number=10
      prog.x(input_qubit[0]) # number=11
      prog.cx(input_qubit[1],input_qubit[0]) # number=12
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = BasicAer.get_backend('statevector_simulator')
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      prog = circuit1
  
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
  
      writefile = open("../data/startQiskit_Class230.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=5
  # total number=70
  import cirq
  import qiskit
  from qiskit import IBMQ
  from qiskit.providers.ibmq import least_busy
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      oracle = QuantumCircuit(controls, name="Zf")
  
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
  
              # oracle.h(controls[n])
              if n >= 2:
                  oracle.mcu1(pi, controls[1:], controls[0])
  
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
  
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[0]) # number=3
      prog.h(input_qubit[1]) # number=4
      prog.h(input_qubit[2]) # number=5
      prog.h(input_qubit[1]) # number=29
      prog.cz(input_qubit[3],input_qubit[1]) # number=30
      prog.h(input_qubit[1]) # number=31
      prog.h(input_qubit[3]) # number=6
      prog.h(input_qubit[4])  # number=21
  
      Zf = build_oracle(n, f)
  
      repeat = floor(sqrt(2 ** n) * pi / 4)
      for i in range(repeat):
          prog.append(Zf.to_gate(), [input_qubit[i] for i in range(n)])
          prog.h(input_qubit[0])  # number=1
          prog.h(input_qubit[1])  # number=2
          prog.h(input_qubit[2])  # number=7
          prog.h(input_qubit[3])  # number=8
  
  
          prog.h(input_qubit[0])  # number=38
          prog.cz(input_qubit[1],input_qubit[0])  # number=39
          prog.h(input_qubit[0])  # number=40
          prog.h(input_qubit[0])  # number=51
          prog.cz(input_qubit[1],input_qubit[0])  # number=52
          prog.h(input_qubit[0])  # number=53
          prog.h(input_qubit[0])  # number=64
          prog.cz(input_qubit[1],input_qubit[0])  # number=65
          prog.h(input_qubit[0])  # number=66
          prog.x(input_qubit[0])  # number=49
          prog.h(input_qubit[0])  # number=57
          prog.cz(input_qubit[1],input_qubit[0])  # number=58
          prog.h(input_qubit[0])  # number=59
          prog.h(input_qubit[0])  # number=54
          prog.cz(input_qubit[1],input_qubit[0])  # number=55
          prog.h(input_qubit[0])  # number=56
          prog.h(input_qubit[4]) # number=41
          prog.h(input_qubit[0])  # number=61
          prog.cz(input_qubit[1],input_qubit[0])  # number=62
          prog.h(input_qubit[0])  # number=63
          prog.cx(input_qubit[0],input_qubit[1])  # number=67
          prog.x(input_qubit[1])  # number=68
          prog.cx(input_qubit[0],input_qubit[1])  # number=69
          prog.h(input_qubit[2])  # number=25
          prog.cz(input_qubit[0],input_qubit[2])  # number=26
          prog.h(input_qubit[2])  # number=27
          prog.x(input_qubit[2])  # number=23
          prog.cx(input_qubit[0],input_qubit[2])  # number=24
          prog.cx(input_qubit[0],input_qubit[3])  # number=32
          prog.x(input_qubit[3])  # number=33
          prog.h(input_qubit[3])  # number=42
          prog.cz(input_qubit[0],input_qubit[3])  # number=43
          prog.h(input_qubit[3])  # number=44
  
          if n>=2:
              prog.mcu1(pi,input_qubit[1:],input_qubit[0])
  
          prog.x(input_qubit[0])  # number=13
          prog.rx(0.6157521601035993,input_qubit[1]) # number=60
          prog.x(input_qubit[1])  # number=14
          prog.x(input_qubit[2])  # number=15
          prog.x(input_qubit[3])  # number=16
  
  
          prog.h(input_qubit[0])  # number=17
          prog.h(input_qubit[1])  # number=18
          prog.h(input_qubit[2])  # number=19
          prog.h(input_qubit[3])  # number=20
  
  
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  
  if __name__ == '__main__':
      key = "00000"
      f = lambda rep: str(int(rep == key))
      prog = make_circuit(5,f)
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = least_busy(provider.backends(filters=lambda x: x.configuration().n_qubits >= 2 and not x.configuration().simulator and x.status().operational == True))
      sample_shot =7924
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_QC1786.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=12
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  import networkx as nx
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def make_circuit(n:int) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      prog = QuantumCircuit(input_qubit)
      prog.h(input_qubit[0]) # number=1
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2])  # number=3
      prog.cx(input_qubit[0],input_qubit[2]) # number=9
      prog.x(input_qubit[2]) # number=10
      prog.cx(input_qubit[0],input_qubit[2]) # number=11
      prog.h(input_qubit[3])  # number=4
      prog.y(input_qubit[3]) # number=5
  
      for edge in E:
          k = edge[0]
          l = edge[1]
          prog.cp(-2 * gamma, input_qubit[k-1], input_qubit[l-1])
          prog.p(gamma, k)
          prog.p(gamma, l)
  
      prog.rx(2 * beta, range(len(V)))
  
      prog.swap(input_qubit[1],input_qubit[0]) # number=7
      prog.swap(input_qubit[1],input_qubit[0]) # number=8
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      n = 4
      V = np.arange(0, n, 1)
      E = [(0, 1, 1.0), (0, 2, 1.0), (1, 2, 1.0), (3, 2, 1.0), (3, 1, 1.0)]
  
      G = nx.Graph()
      G.add_nodes_from(V)
      G.add_weighted_edges_from(E)
  
      step_size = 0.1
  
      a_gamma = np.arange(0, np.pi, step_size)
      a_beta = np.arange(0, np.pi, step_size)
      a_gamma, a_beta = np.meshgrid(a_gamma, a_beta)
  
      F1 = 3 - (np.sin(2 * a_beta) ** 2 * np.sin(2 * a_gamma) ** 2 - 0.5 * np.sin(4 * a_beta) * np.sin(4 * a_gamma)) * (
                  1 + np.cos(4 * a_gamma) ** 2)
  
      result = np.where(F1 == np.amax(F1))
      a = list(zip(result[0], result[1]))[0]
  
      gamma = a[0] * step_size
      beta = a[1] * step_size
  
      prog = make_circuit(4)
      sample_shot =5600
      writefile = open("../data/startQiskit_noisy116.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      backend = FakeYorktown()
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.measure_all()
      prog = circuit1
  
      info = execute(prog,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=5
  # total number=59
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      oracle = QuantumCircuit(controls, name="Zf")
  
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
  
              # oracle.h(controls[n])
              if n >= 2:
                  oracle.mcu1(pi, controls[1:], controls[0])
  
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
  
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[0]) # number=3
      prog.h(input_qubit[1]) # number=4
      prog.h(input_qubit[2]) # number=5
      prog.h(input_qubit[3]) # number=6
      prog.h(input_qubit[0]) # number=41
      prog.cz(input_qubit[1],input_qubit[0]) # number=42
      prog.h(input_qubit[0]) # number=43
      prog.z(input_qubit[1]) # number=37
      prog.h(input_qubit[0]) # number=51
      prog.cz(input_qubit[1],input_qubit[0]) # number=52
      prog.h(input_qubit[0]) # number=53
      prog.h(input_qubit[4])  # number=21
      prog.x(input_qubit[2]) # number=39
  
      Zf = build_oracle(n, f)
  
      repeat = floor(sqrt(2 ** n) * pi / 4)
      for i in range(repeat):
          prog.append(Zf.to_gate(), [input_qubit[i] for i in range(n)])
          prog.h(input_qubit[0])  # number=1
          prog.h(input_qubit[1])  # number=2
          prog.h(input_qubit[2])  # number=7
          prog.h(input_qubit[3])  # number=8
          prog.h(input_qubit[0]) # number=56
          prog.cz(input_qubit[3],input_qubit[0]) # number=57
          prog.h(input_qubit[0]) # number=58
          prog.h(input_qubit[0]) # number=48
          prog.cz(input_qubit[3],input_qubit[0]) # number=49
          prog.h(input_qubit[0]) # number=50
          prog.z(input_qubit[3]) # number=46
          prog.cx(input_qubit[3],input_qubit[0]) # number=47
          prog.x(input_qubit[4]) # number=40
          prog.cx(input_qubit[3],input_qubit[0]) # number=35
  
  
          prog.x(input_qubit[0])  # number=9
          prog.cx(input_qubit[0],input_qubit[1])  # number=29
          prog.x(input_qubit[1])  # number=30
          prog.cx(input_qubit[0],input_qubit[1])  # number=31
          prog.x(input_qubit[2])  # number=11
          prog.x(input_qubit[1]) # number=44
          prog.x(input_qubit[3])  # number=12
  
          if n>=2:
              prog.mcu1(pi,input_qubit[1:],input_qubit[0])
  
          prog.cx(input_qubit[1],input_qubit[0])  # number=24
          prog.x(input_qubit[0])  # number=25
          prog.cx(input_qubit[1],input_qubit[0])  # number=26
          prog.x(input_qubit[1])  # number=14
          prog.x(input_qubit[2])  # number=15
          prog.x(input_qubit[3])  # number=16
  
  
          prog.h(input_qubit[0])  # number=17
          prog.h(input_qubit[1])  # number=18
          prog.cx(input_qubit[4],input_qubit[3]) # number=54
          prog.h(input_qubit[2])  # number=19
          prog.h(input_qubit[3])  # number=20
  
  
      prog.x(input_qubit[1]) # number=22
      prog.y(input_qubit[1]) # number=32
      prog.x(input_qubit[1]) # number=23
      prog.cx(input_qubit[4],input_qubit[3]) # number=55
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  
  if __name__ == '__main__':
      key = "00000"
      f = lambda rep: str(int(rep == key))
      prog = make_circuit(5,f)
      backend = BasicAer.get_backend('qasm_simulator')
      sample_shot =7924
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit1740.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=34
  import cirq
  import qiskit
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.cx(input_qubit[0],input_qubit[3]) # number=13
      prog.cx(input_qubit[0],input_qubit[3]) # number=17
      prog.x(input_qubit[3]) # number=18
      prog.cx(input_qubit[0],input_qubit[3]) # number=19
      prog.cx(input_qubit[0],input_qubit[3]) # number=15
      prog.h(input_qubit[1]) # number=2
      prog.cx(input_qubit[2],input_qubit[1]) # number=27
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.y(input_qubit[3]) # number=12
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[0]) # number=24
      prog.cz(input_qubit[3],input_qubit[0]) # number=25
      prog.h(input_qubit[0]) # number=26
      prog.h(input_qubit[0]) # number=31
      prog.cz(input_qubit[3],input_qubit[0]) # number=32
      prog.h(input_qubit[0]) # number=33
      prog.z(input_qubit[3]) # number=29
      prog.cx(input_qubit[3],input_qubit[0]) # number=30
      prog.x(input_qubit[2]) # number=23
      prog.cx(input_qubit[3],input_qubit[0]) # number=22
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[2]) # number=10
      prog.y(input_qubit[2]) # number=11
      # circuit end
  
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = BasicAer.get_backend('statevector_simulator')
      sample_shot =8000
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_Class2002.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=10
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.cx(input_qubit[0],input_qubit[1]) # number=2
      prog.cx(input_qubit[0],input_qubit[1]) # number=5
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      prog.x(input_qubit[0]) # number=3
      prog.y(input_qubit[1]) # number=6
      prog.cx(input_qubit[1],input_qubit[0]) # number=7
      prog.x(input_qubit[0]) # number=8
      prog.cx(input_qubit[1],input_qubit[0]) # number=9
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = BasicAer.get_backend('statevector_simulator')
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      prog = circuit1
  
  
      info = execute(prog, backend=backend).result().get_statevector()
      qubits = round(log2(len(info)))
      info = {
          np.binary_repr(i, qubits): round((info[i]*(info[i].conjugate())).real,3)
          for i in range(2 ** qubits)
      }
  
      writefile = open("../data/startQiskit_Class145.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=3
  # total number=31
  
  import numpy as np
  
  from qiskit import QuantumCircuit, execute, Aer, QuantumRegister, ClassicalRegister, transpile, BasicAer, IBMQ
  from qiskit.visualization import plot_histogram
  from typing import *
  from pprint import pprint
  from math import log2
  from collections import Counter
  from qiskit.test.mock import FakeVigo, FakeYorktown
  
  kernel = 'circuit/bernstein'
  
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  
  def build_oracle(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename=(kernel + '-oracle.png'))
      return oracle
  
  
  def build_circuit(n: int, f: Callable[[str], str]) -> QuantumCircuit:
      # implement the Bernstein-Vazirani circuit
      zero = np.binary_repr(0, n)
      b = f(zero)
  
      # initial n + 1 bits
      input_qubit = QuantumRegister(n+1, "qc")
      classicals = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classicals)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(input_qubit[n])
      # circuit begin
      prog.h(input_qubit[1])  # number=1
      prog.rx(-0.09738937226128368,input_qubit[2]) # number=2
      prog.cx(input_qubit[2],input_qubit[1]) # number=27
      prog.h(input_qubit[1]) # number=3
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
      prog.h(input_qubit[n])
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [input_qubit[n]])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
  
      return prog
  
  
  def get_statevector(prog: QuantumCircuit) -> Any:
      state_backend = Aer.get_backend('statevector_simulator')
      statevec = execute(prog, state_backend).result()
      quantum_state = statevec.get_statevector()
      qubits = round(log2(len(quantum_state)))
      quantum_state = {
          "|" + np.binary_repr(i, qubits) + ">": quantum_state[i]
          for i in range(2 ** qubits)
      }
      return quantum_state
  
  
  def evaluate(backend_str: str, prog: QuantumCircuit, shots: int, b: str) -> Any:
      # Q: which backend should we use?
  
      # get state vector
      quantum_state = get_statevector(prog)
  
      # get simulate results
  
      # provider = IBMQ.load_account()
      # backend = provider.get_backend(backend_str)
      # qobj = compile(prog, backend, shots)
      # job = backend.run(qobj)
      # job.result()
      backend = Aer.get_backend(backend_str)
      # transpile/schedule -> assemble -> backend.run
      results = execute(prog, backend, shots=shots).result()
      counts = results.get_counts()
      a = Counter(counts).most_common(1)[0][0][::-1]
  
      return {
          "measurements": counts,
          # "state": statevec,
          "quantum_state": quantum_state,
          "a": a,
          "b": b
      }
  
  
  def bernstein_test_1(rep: str):
      """011 . x + 1"""
      a = "011"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_2(rep: str):
      """000 . x + 0"""
      a = "000"
      b = "0"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  def bernstein_test_3(rep: str):
      """111 . x + 1"""
      a = "111"
      b = "1"
      return bitwise_xor(bitwise_dot(a, rep), b)
  
  
  if __name__ == "__main__":
      n = 2
      a = "11"
      b = "1"
      f = lambda rep: \
          bitwise_xor(bitwise_dot(a, rep), b)
      prog = build_circuit(n, f)
      sample_shot =4000
      writefile = open("../data/startQiskit_Class179.csv", "w")
      # prog.draw('mpl', filename=(kernel + '.png'))
      backend = BasicAer.get_backend('statevector_simulator')
  
      circuit1 = transpile(prog, FakeYorktown())
      circuit1.h(qubit=2)
      circuit1.x(qubit=3)
  
      info = execute(circuit1,backend=backend, shots=sample_shot).result().get_counts()
  
      print(info, file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(), file=writefile)
      print(circuit1, file=writefile)
      writefile.close()
   # qubit number=5
  # total number=69
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      oracle = QuantumCircuit(controls, name="Zf")
  
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
  
              # oracle.h(controls[n])
              if n >= 2:
                  oracle.mcu1(pi, controls[1:], controls[0])
  
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
  
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[0]) # number=3
      prog.h(input_qubit[1]) # number=4
      prog.h(input_qubit[0]) # number=57
      prog.cz(input_qubit[4],input_qubit[0]) # number=58
      prog.h(input_qubit[0]) # number=59
      prog.z(input_qubit[4]) # number=55
      prog.cx(input_qubit[4],input_qubit[0]) # number=56
      prog.h(input_qubit[2]) # number=50
      prog.cz(input_qubit[4],input_qubit[2]) # number=51
      prog.h(input_qubit[2]) # number=52
      prog.h(input_qubit[2]) # number=5
      prog.h(input_qubit[3]) # number=6
      prog.h(input_qubit[4])  # number=21
  
      Zf = build_oracle(n, f)
  
      repeat = floor(sqrt(2 ** n) * pi / 4)
      for i in range(repeat):
          prog.append(Zf.to_gate(), [input_qubit[i] for i in range(n)])
          prog.h(input_qubit[0])  # number=1
          prog.h(input_qubit[1])  # number=2
          prog.h(input_qubit[2])  # number=7
          prog.h(input_qubit[3])  # number=8
  
  
          prog.h(input_qubit[0])  # number=28
          prog.h(input_qubit[0]) # number=66
          prog.cz(input_qubit[3],input_qubit[0]) # number=67
          prog.h(input_qubit[0]) # number=68
          prog.z(input_qubit[3]) # number=61
          prog.cx(input_qubit[3],input_qubit[0]) # number=62
          prog.cz(input_qubit[1],input_qubit[0])  # number=29
          prog.h(input_qubit[0])  # number=30
          prog.h(input_qubit[0])  # number=43
          prog.cz(input_qubit[1],input_qubit[0])  # number=44
          prog.h(input_qubit[0])  # number=45
          prog.cx(input_qubit[1],input_qubit[0])  # number=35
          prog.cx(input_qubit[1],input_qubit[0])  # number=38
          prog.x(input_qubit[0])  # number=39
          prog.cx(input_qubit[1],input_qubit[0])  # number=40
          prog.cx(input_qubit[1],input_qubit[0])  # number=37
          prog.h(input_qubit[0])  # number=46
          prog.cz(input_qubit[1],input_qubit[0])  # number=47
          prog.h(input_qubit[0])  # number=48
          prog.h(input_qubit[0])  # number=63
          prog.cz(input_qubit[1],input_qubit[0])  # number=64
          prog.h(input_qubit[0])  # number=65
          prog.x(input_qubit[1])  # number=10
          prog.x(input_qubit[2])  # number=11
          prog.x(input_qubit[3])  # number=12
  
          if n>=2:
              prog.mcu1(pi,input_qubit[1:],input_qubit[0])
  
          prog.x(input_qubit[0])  # number=13
          prog.cx(input_qubit[0],input_qubit[1])  # number=22
          prog.y(input_qubit[2]) # number=41
          prog.x(input_qubit[1])  # number=23
          prog.cx(input_qubit[0],input_qubit[1])  # number=24
          prog.rx(1.0398671683382215,input_qubit[2]) # number=31
          prog.x(input_qubit[2])  # number=15
          prog.x(input_qubit[3])  # number=16
  
  
          prog.h(input_qubit[0])  # number=17
          prog.h(input_qubit[1])  # number=18
          prog.h(input_qubit[2])  # number=19
          prog.h(input_qubit[3])  # number=20
  
  
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  
  if __name__ == '__main__':
      key = "00000"
      f = lambda rep: str(int(rep == key))
      prog = make_circuit(5,f)
      backend = FakeVigo()
      sample_shot =7924
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_noisy1883.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=9
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.x(input_qubit[1]) # number=5
      prog.cx(input_qubit[0],input_qubit[1]) # number=4
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
      #for i in range(n):
      #    prog.measure(input_qubit[i], classicals[i])
  
      prog.cx(input_qubit[0],input_qubit[1]) # number=6
      prog.x(input_qubit[1]) # number=7
      prog.cx(input_qubit[0],input_qubit[1]) # number=8
      prog.x(input_qubit[1]) # number=3
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      IBMQ.load_account() 
      provider = IBMQ.get_provider(hub='ibm-q') 
      provider.backends()
      backend = provider.get_backend("ibmq_belem")
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      circuit1.measure_all()
      prog = circuit1
  
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
  
      writefile = open("../data/startQiskit_QC79.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=7
  import cirq
  import qiskit
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
      #for i in range(n):
      #    prog.measure(input_qubit[i], classicals[i])
  
      prog.y(input_qubit[1]) # number=2
      prog.y(input_qubit[1]) # number=4
      prog.y(input_qubit[1]) # number=3
      prog.swap(input_qubit[1],input_qubit[0]) # number=5
      prog.swap(input_qubit[1],input_qubit[0]) # number=6
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = BasicAer.get_backend('qasm_simulator')
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      circuit1.measure_all()
      prog = circuit1
  
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
  
      writefile = open("../data/startQiskit78.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=2
  # total number=9
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import IBMQ
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2,floor, sqrt, pi
  import numpy as np
  import networkx as nx
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f^\pm
      # NOTE: use U1 gate (P gate) with \lambda = 180 ==> CZ gate
      # or multi_control_Z_gate (issue #127)
  
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      # oracle.draw('mpl', filename='circuit/deutsch-oracle.png')
      return oracle
  
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
  
      input_qubit = QuantumRegister(n, "qc")
      target = QuantumRegister(1, "qt")
      prog = QuantumCircuit(input_qubit, target)
  
      # inverse last one (can be omitted if using O_f^\pm)
      prog.x(target)
  
      # apply H to get superposition
      for i in range(n):
          prog.h(input_qubit[i])
  
      prog.h(input_qubit[1]) # number=1
      prog.h(input_qubit[1]) # number=4
      prog.h(target)
      prog.barrier()
  
      # apply oracle O_f
      oracle = build_oracle(n, f)
      prog.append(
          oracle.to_gate(),
          [input_qubit[i] for i in range(n)] + [target])
  
      # apply H back (QFT on Z_2^n)
      for i in range(n):
          prog.h(input_qubit[i])
      prog.barrier()
  
      # measure
      #for i in range(n):
      #    prog.measure(input_qubit[i], classicals[i])
  
      prog.x(input_qubit[1]) # number=2
      prog.x(input_qubit[1]) # number=3
      prog.cx(input_qubit[1],input_qubit[0]) # number=5
      prog.cx(input_qubit[1],input_qubit[0]) # number=6
      prog.y(input_qubit[1]) # number=7
      prog.y(input_qubit[1]) # number=8
      # circuit end
      return prog
  
  
  
  
  if __name__ == '__main__':
      n = 2
      f = lambda rep: rep[-1]
      # f = lambda rep: "1" if rep[0:2] == "01" or rep[0:2] == "10" else "0"
      # f = lambda rep: "0"
      prog = make_circuit(n, f)
      sample_shot =2800
      backend = FakeVigo()
  
      circuit1 = transpile(prog,FakeVigo())
      circuit1.x(qubit=3)
      circuit1.x(qubit=3)
      circuit1.measure_all()
      prog = circuit1
  
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
  
      writefile = open("../data/startQiskit_noisy126.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.depth(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
   # qubit number=4
  # total number=43
  import cirq
  import qiskit
  from qiskit.providers.aer import QasmSimulator
  from qiskit.test.mock import FakeVigo
  
  from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister
  from qiskit import BasicAer, execute, transpile
  from pprint import pprint
  from qiskit.test.mock import FakeVigo
  from math import log2
  import numpy as np
  import networkx as nx
  
  def bitwise_xor(s: str, t: str) -> str:
      length = len(s)
      res = []
      for i in range(length):
          res.append(str(int(s[i]) ^ int(t[i])))
      return ''.join(res[::-1])
  
  
  def bitwise_dot(s: str, t: str) -> str:
      length = len(s)
      res = 0
      for i in range(length):
          res += int(s[i]) * int(t[i])
      return str(res % 2)
  
  def build_oracle(n: int, f) -> QuantumCircuit:
      # implement the oracle O_f
      # NOTE: use multi_control_toffoli_gate ('noancilla' mode)
      # https://qiskit.org/documentation/_modules/qiskit/aqua/circuits/gates/multi_control_toffoli_gate.html
      # https://quantumcomputing.stackexchange.com/questions/3943/how-do-you-implement-the-toffoli-gate-using-only-single-qubit-and-cnot-gates
      # https://quantumcomputing.stackexchange.com/questions/2177/how-can-i-implement-an-n-bit-toffoli-gate
      controls = QuantumRegister(n, "ofc")
      target = QuantumRegister(1, "oft")
      oracle = QuantumCircuit(controls, target, name="Of")
      for i in range(2 ** n):
          rep = np.binary_repr(i, n)
          if f(rep) == "1":
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              oracle.mct(controls, target[0], None, mode='noancilla')
              for j in range(n):
                  if rep[j] == "0":
                      oracle.x(controls[j])
              # oracle.barrier()
      return oracle
  
  def make_circuit(n:int,f) -> QuantumCircuit:
      # circuit begin
      input_qubit = QuantumRegister(n,"qc")
      classical = ClassicalRegister(n, "qm")
      prog = QuantumCircuit(input_qubit, classical)
      prog.h(input_qubit[3]) # number=16
      prog.cz(input_qubit[0],input_qubit[3]) # number=17
      prog.rx(-0.5686282702997527,input_qubit[3]) # number=32
      prog.h(input_qubit[3]) # number=18
      prog.h(input_qubit[3]) # number=26
      prog.cz(input_qubit[0],input_qubit[3]) # number=27
      prog.h(input_qubit[3]) # number=28
      prog.x(input_qubit[3]) # number=21
      prog.rx(0.4241150082346221,input_qubit[2]) # number=33
      prog.cx(input_qubit[0],input_qubit[3]) # number=22
      prog.cx(input_qubit[0],input_qubit[3]) # number=12
      prog.h(input_qubit[1]) # number=2
      prog.h(input_qubit[2]) # number=3
      prog.h(input_qubit[3]) # number=4
      prog.h(input_qubit[0]) # number=5
  
      oracle = build_oracle(n-1, f)
      prog.append(oracle.to_gate(),[input_qubit[i] for i in range(n-1)]+[input_qubit[n-1]])
      prog.h(input_qubit[1])  # number=6
      prog.h(input_qubit[2]) # number=23
      prog.cz(input_qubit[1],input_qubit[2]) # number=24
      prog.h(input_qubit[2]) # number=25
      prog.h(input_qubit[2])  # number=7
      prog.h(input_qubit[3])  # number=8
      prog.h(input_qubit[0]) # number=34
      prog.cz(input_qubit[2],input_qubit[0]) # number=35
      prog.h(input_qubit[0]) # number=36
      prog.cx(input_qubit[2],input_qubit[0]) # number=37
      prog.cx(input_qubit[2],input_qubit[0]) # number=40
      prog.z(input_qubit[2]) # number=41
      prog.cx(input_qubit[2],input_qubit[0]) # number=42
      prog.cx(input_qubit[2],input_qubit[0]) # number=39
      prog.cx(input_qubit[2],input_qubit[0]) # number=31
      prog.h(input_qubit[0])  # number=9
  
      prog.y(input_qubit[0]) # number=14
      prog.y(input_qubit[0]) # number=15
      # circuit end
  
      for i in range(n):
          prog.measure(input_qubit[i], classical[i])
  
  
      return prog
  
  
  
  if __name__ == '__main__':
      a = "111"
      b = "0"
      f = lambda rep: bitwise_xor(bitwise_dot(a, rep), b)
      prog = make_circuit(4,f)
      backend = FakeVigo()
      sample_shot =8000
  
      info = execute(prog, backend=backend, shots=sample_shot).result().get_counts()
      backend = FakeVigo()
      circuit1 = transpile(prog,backend,optimization_level=2)
  
      writefile = open("../data/startQiskit_noisy2692.csv","w")
      print(info,file=writefile)
      print("results end", file=writefile)
      print(circuit1.__len__(),file=writefile)
      print(circuit1,file=writefile)
      writefile.close()
  
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.cos numpy.arange numpy.amax numpy.sin numpy.meshgrid
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.cos numpy.arange numpy.amax numpy.sin numpy.meshgrid
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.cos numpy.arange numpy.amax numpy.sin numpy.meshgrid
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.cos numpy.arange numpy.amax numpy.sin numpy.meshgrid
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.cos numpy.arange numpy.amax numpy.sin numpy.meshgrid
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.cos numpy.arange numpy.amax numpy.sin numpy.meshgrid
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr
apis: numpy.binary_repr