Result(device: str, backend: str, precision: str, circuit_record: bool, amplitude_record: bool, options: dict, output_path: str = None)
Data structure class for simulator result
Parameters:
-
mode
(str)
–
The simulator mode, usually be {device-backend}.
-
shots
(int)
–
The running times; must be a positive integer.
-
options
(dict)
–
Optional parameters for the simulator.
Source code in QuICT/simulation/utils/result.py
| def __init__(
self,
device: str,
backend: str,
precision: str,
circuit_record: bool,
amplitude_record: bool,
options: dict,
output_path: str = None
):
self.device = device
self.backend = backend
self.precision = precision
self._circuit_record = circuit_record
self._amplitude_record = amplitude_record
self.options = options
self.counts = {}
self.state_vector = None
self.density_matrix = None
# prepare output path
self._dump_folder = True if self._amplitude_record or self._circuit_record else False
if self._dump_folder:
self._output_path = self._prepare_output_file(output_path)
|
record_amplitude
record_amplitude(amplitude)
dump the circuit.
Source code in QuICT/simulation/utils/result.py
| def record_amplitude(self, amplitude):
""" dump the circuit. """
if self.device == "GPU":
amplitude = amplitude.get()
if self._amplitude_record:
np.savetxt(f"{self._output_path}/amp_{self.id}.txt", amplitude)
if self.backend == "density_matrix":
self.qubit_num = int(np.log2(amplitude.shape[0]))
self.density_matrix = amplitude.copy()
else:
self.qubit_num = int(np.log2(amplitude.shape[0]))
self.state_vector = amplitude.copy()
|
record_circuit
dump the circuit.
Source code in QuICT/simulation/utils/result.py
| def record_circuit(self, circuit):
""" dump the circuit. """
self.id = circuit.name
self.qubit_num = circuit.width()
if self._circuit_record:
with open(f"{self._output_path}/circuit_{self.id}.qasm", "w") as of:
of.write(circuit.qasm())
|
record_sample
record_sample(result: dict)
Record circuit's result
Parameters:
-
result
(dict)
–
The sample of measured result from given circuit.
Source code in QuICT/simulation/utils/result.py
| def record_sample(self, result: dict):
""" Record circuit's result
Args:
result (dict): The sample of measured result from given circuit.
"""
self.shots = 0
for key, value in result.items():
bit_idx = "{0:0b}".format(key).zfill(self.qubit_num)
self.counts[bit_idx] = value
if self._dump_folder:
with open(f"{self._output_path}/result_{self.id}.log", "w") as of:
of.write(str(self.__dict__()))
|