Skip to content

Commit

Permalink
add qpu conf
Browse files Browse the repository at this point in the history
  • Loading branch information
liwt31 committed Jan 15, 2024
1 parent 08b2794 commit 0c6a760
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
20 changes: 11 additions & 9 deletions tencirchem/static/engine_hea.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
from tencirchem.utils.backend import jit


class QpuConf:
def __init__(self, device=None, provider=None, initial_mapping=None):
if device is None:
device = "tianji_s2"
self.device = device
self.privider = provider
self.initial_mapping = initial_mapping


@partial(jit, static_argnums=[1])
def get_statevector(params, get_circuit):
return get_circuit(params).state()
Expand Down Expand Up @@ -73,7 +82,7 @@ def get_energy_tensornetwork_noise_shot(params, paulis, coeffs, get_dmcircuit, n
return sample_expectation_pauli(c, paulis, coeffs, shots, noise_conf)


def get_energy_qpu(params, paulis, coeffs, get_circuit, shots: int):
def get_energy_qpu(params, paulis, coeffs, get_circuit, qpu_conf: QpuConf, shots: int):
c: Circuit = get_circuit(params)
pss = []
symbol_mapping = {"X": 1, "Y": 2, "Z": 3}
Expand All @@ -91,7 +100,7 @@ def get_energy_qpu(params, paulis, coeffs, get_circuit, shots: int):
assert len(pss) == len(coeffs_non_identity)
es = []
for _ in range((shots - 1) // 8192 + 1):
e = batch_expectation_ps(c, pss, device="tianji_s2", ws=coeffs_non_identity, shots=8192)
e = batch_expectation_ps(c, pss, device=qpu_conf.device, ws=coeffs_non_identity, shots=8192)
es.append(e)
print(paulis)
print(coeffs)
Expand Down Expand Up @@ -158,10 +167,3 @@ def get_energy_and_grad_qpu(params, paulis, coeffs, get_circuit, shots: int, gra
shots=shots,
)
return _get_energy_and_grad(partial_get_energy, params, grad)


class QpuConf:
def __init__(self, device=None, provider=None, initial_mapping=None):
self.device = device
self.privider = provider
self.initial_mapping = initial_mapping
11 changes: 8 additions & 3 deletions tencirchem/static/hea.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from tensorcircuit.noisemodel import NoiseConf, circuit_with_noise

from tencirchem.static.engine_hea import (
QpuConf,
get_statevector,
get_densitymatrix,
get_energy_tensornetwork,
Expand Down Expand Up @@ -436,7 +437,7 @@ def __init__(
circuit: Union[Callable, QuantumCircuit],
init_guess: Union[List[float], np.ndarray],
engine: str = None,
engine_conf: NoiseConf = None,
engine_conf: [NoiseConf, QpuConf] = None,
):
"""
Construct the HEA class from Hamiltonian in :class:`QubitOperator` form and the ansatz.
Expand All @@ -463,8 +464,11 @@ def __init__(
engine = "tensornetwork"
if engine == "tensornetwork" and engine_conf is not None:
raise ValueError("Tensornetwork engine does not have engine configuration")
if engine.startswith("tensornetwork-noise") and engine_conf is None:
engine_conf = get_noise_conf()
if engine_conf is None:
if engine.startswith("tensornetwork-noise"):
engine_conf = get_noise_conf()
if engine.startswith("qpu"):
engine_conf = QpuConf()

init_guess = np.array(init_guess)

Expand Down Expand Up @@ -738,6 +742,7 @@ def energy(self, params: Tensor = None, engine: str = None) -> float:
tuple(self.h_qubit_op.terms.keys()),
list(self.h_qubit_op.terms.values()),
self.get_circuit,
self.engine_conf,
self.shots,
)
return e
Expand Down

0 comments on commit 0c6a760

Please sign in to comment.