Skip to content

Commit d960eb8

Browse files
Correction: Custom Gate Parameter parser and Real Warning in Pauli String real
1 parent 82a5066 commit d960eb8

File tree

4 files changed

+76
-43
lines changed

4 files changed

+76
-43
lines changed

mpqp/core/instruction/measurement/pauli_string.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def round(self, round_off_till: int = 5) -> PauliString:
183183
"""
184184
res = PauliString()
185185
for mono in self.monomials:
186-
coef = float(round(mono.coef, round_off_till))
186+
coef = float(np.round(float(mono.coef.real), round_off_till))
187187
if coef != 0:
188188
res.monomials.append(PauliStringMonomial(coef, mono.atoms))
189189
if len(res.monomials) == 0:
@@ -244,7 +244,7 @@ def from_matrix(cls, matrix: Matrix) -> PauliString:
244244

245245
pauli_list = PauliString()
246246
for i, mat in enumerate(basis):
247-
coeff = np.trace(mat.to_matrix().dot(matrix)) / (2**num_qubits)
247+
coeff = (np.trace(mat.to_matrix().dot(matrix)) / (2**num_qubits)).real
248248
if not np.isclose(coeff, 0, atol=atol, rtol=rtol):
249249
mono = basis[i] * coeff
250250
pauli_list += mono

mpqp/execution/providers/google.py

+67-36
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,69 @@ def run_google(job: Job) -> Result:
4949
Returns:
5050
A Result after submission and execution of the job.
5151
"""
52-
return run_local(job) if not job.device.is_remote() else run_google_remote(job)
52+
if type(job.device) != GOOGLEDevice:
53+
raise ValueError("Job device must be GOOGLEDevice")
54+
55+
if job.device.is_processor():
56+
return run_processor(job)
57+
elif job.device.is_remote():
58+
return run_google_remote(job)
59+
else:
60+
return run_local(job)
61+
62+
63+
@typechecked
64+
def run_processor(job: Job) -> Result:
65+
"""
66+
Executes the job locally on processor.
67+
68+
Args:
69+
job : The job to be executed.
70+
71+
Returns:
72+
Result: The result after submission and execution of the job..
73+
"""
74+
cal = load_median_device_calibration(job.device.value)
75+
device = create_device_from_processor_id(job.device.value)
76+
# noise_props = noise_properties_from_calibration(cal)
77+
# noise_model = NoiseModelFromGoogleNoiseProperties(noise_props)
78+
sim = QSimSimulator(noise=None)
79+
sim_processor = SimulatedLocalProcessor(
80+
processor_id=job.device.value,
81+
sampler=sim,
82+
device=device,
83+
calibrations={cal.timestamp // 1000: cal},
84+
)
85+
sim = SimulatedLocalEngine([sim_processor])
86+
87+
job_cirq_circuit = job.circuit.to_other_language(
88+
Language.CIRQ, processor_id=job.device.value
89+
)
90+
if not isinstance(job_cirq_circuit, Cirq_circuit):
91+
raise ValueError("Circuit must be Cirq_circuit")
92+
93+
if job.job_type == JobType.STATE_VECTOR:
94+
raise NotImplementedError(
95+
f"Does not handle {job.job_type} for processor for the moment"
96+
)
97+
elif job.job_type == JobType.OBSERVABLE:
98+
raise NotImplementedError(
99+
f"Does not handle {job.job_type} for processor for the moment"
100+
)
101+
elif job.job_type == JobType.SAMPLE:
102+
assert isinstance(job.measure, BasisMeasure)
103+
if isinstance(job.measure.basis, ComputationalBasis):
104+
result_sim = sim.get_sampler(job.device.value).run(
105+
job_cirq_circuit, repetitions=job.measure.shots
106+
)
107+
else:
108+
raise NotImplementedError(
109+
"Does not handle other basis than the ComputationalBasis for the moment"
110+
)
111+
else:
112+
raise ValueError(f"Job type {job.job_type} not handled")
113+
114+
return extract_result(result_sim, job, GOOGLEDevice.CIRQ_LOCAL_SIMULATOR)
53115

54116

55117
@typechecked
@@ -110,53 +172,23 @@ def run_local(job: Job) -> Result:
110172
if type(job.device) != GOOGLEDevice:
111173
raise ValueError("Job device must be GOOGLEDevice")
112174

113-
if job.device.is_processor():
114-
job_cirq_circuit = job.circuit.to_other_language(
115-
Language.CIRQ, processor_id=job.device.value
116-
)
117-
else:
118-
job_cirq_circuit = job.circuit.to_other_language(Language.CIRQ)
175+
job_cirq_circuit = job.circuit.to_other_language(Language.CIRQ)
176+
119177
if not isinstance(job_cirq_circuit, Cirq_circuit):
120178
raise ValueError("Circuit must be Cirq_circuit")
121179

122180
sim = Simulator()
123181

124182
if job.job_type == JobType.STATE_VECTOR:
125-
if job.device.is_processor():
126-
raise NotImplementedError(
127-
f"Does not handle {job.job_type} for processor for the moment"
128-
)
129183
result_sim = sim.simulate(job_cirq_circuit)
130-
result = extract_result(result_sim, job, GOOGLEDevice.CIRQ_LOCAL_SIMULATOR)
131184
elif job.job_type == JobType.SAMPLE:
132185
assert isinstance(job.measure, BasisMeasure)
133-
134186
if isinstance(job.measure.basis, ComputationalBasis):
135-
if job.device.is_processor():
136-
cal = load_median_device_calibration(job.device.value)
137-
device = create_device_from_processor_id(job.device.value)
138-
# noise_props = noise_properties_from_calibration(cal)
139-
# noise_model = NoiseModelFromGoogleNoiseProperties(noise_props)
140-
sim = QSimSimulator(noise=None)
141-
142-
sim_processor = SimulatedLocalProcessor(
143-
processor_id=job.device.value,
144-
sampler=sim,
145-
device=device,
146-
calibrations={cal.timestamp // 1000: cal},
147-
)
148-
sim = SimulatedLocalEngine([sim_processor])
149-
150-
result_sim = sim.get_sampler(job.device.value).run(
151-
job_cirq_circuit, repetitions=job.measure.shots
152-
)
153-
else:
154-
result_sim = sim.run(job_cirq_circuit, repetitions=job.measure.shots)
187+
result_sim = sim.run(job_cirq_circuit, repetitions=job.measure.shots)
155188
else:
156189
raise NotImplementedError(
157190
"Does not handle other basis than the ComputationalBasis for the moment"
158191
)
159-
result = extract_result(result_sim, job, GOOGLEDevice.CIRQ_LOCAL_SIMULATOR)
160192
elif job.job_type == JobType.OBSERVABLE:
161193
assert isinstance(job.measure, ExpectationMeasure)
162194

@@ -178,10 +210,9 @@ def run_local(job: Job) -> Result:
178210
sim,
179211
stopping_criteria=RepetitionsStoppingCriteria(job.measure.shots),
180212
)
181-
result = extract_result(result_sim, job, GOOGLEDevice.CIRQ_LOCAL_SIMULATOR)
182213
else:
183214
raise ValueError(f"Job type {job.job_type} not handled")
184-
return result
215+
return extract_result(result_sim, job, GOOGLEDevice.CIRQ_LOCAL_SIMULATOR)
185216

186217

187218
def extract_result(

mpqp/qasm/qasm_remplace_custom_gate.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ def parse_custom_gates(qasm_code: str) -> tuple[dict[str, str], str]:
2525
current_gate_name = line.split()[1]
2626
current_gate_definition = []
2727
current_gate_parameters = [
28-
elem.replace(",", "") for elem in line.split()[2:][:-1]
28+
elem.replace(",", "").replace("{", "")
29+
for elem in line.split()[2:]
30+
if elem.replace(",", "").replace("{", "")
2931
]
3032
current_gate_definition.append(current_gate_parameters)
3133
replaced_code = replaced_code.replace(line, "")
@@ -49,7 +51,7 @@ def replace_custom_gates(qasm_code: str) -> str:
4951
5052
Returns:
5153
str: The QASM code with custom gate calls replaced by their definitions.
52-
54+
5355
Exemple:
5456
>>> qasm_str = \"\"\"gate MyGate a, b {
5557
h a;
@@ -63,7 +65,7 @@ def replace_custom_gates(qasm_code: str) -> str:
6365
6466
measure q -> c;\"\"\"
6567
>>> print(replace_custom_gates(qasm_str))
66-
68+
6769
qreg q[3];
6870
creg c[2];
6971

tests/qasm/test_qasm_to_cirq.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@
6464
}
6565
6666
qreg q[3];
67-
creg c[2];
67+
creg c[3];
6868
6969
MyGate q[0], q[1];
70-
MyGate2 q[0], q[1], q[3];
70+
MyGate2 q[0], q[1], q[2];
7171
7272
measure q -> c;""",
7373
[

0 commit comments

Comments
 (0)