Skip to content

Commit

Permalink
Merge pull request #20 from kbarkhqs/main
Browse files Browse the repository at this point in the history
Homogenising the parametrize vs parameterize syntax
  • Loading branch information
nfwvogt authored May 3, 2021
2 parents baecec0 + e40029e commit b7e185d
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 113 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ target/*
*.DS_Store
*.ipynb_checkpoints*
*.json
*.yaml
*.yaml
htmlcov/*
4 changes: 2 additions & 2 deletions qoqo/circuit/circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,13 +432,13 @@ def _insert_operation(self, index: int, operation: Operation) -> None:
self._operations.insert(index, operation)

@property
def is_parameterized(self) -> bool:
def is_parametrized(self) -> bool:
"""Return True if the circuit has operations with symbolic parameters
Returns:
bool
"""
return any([op.is_parameterized for op in self._operations])
return any([op.is_parametrized for op in self._operations])

def substitute_parameters(self, substitution_dict: Dict[str, float]
) -> None:
Expand Down
5 changes: 4 additions & 1 deletion qoqo/do_unitary/do_unitary.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ def __call__(self,
overwrite=False)
return None
self._backend_cached = self._measurement._backend
parameter_series = pd.Series(parameter_substitution_dict, dtype=float)
if not parameter_substitution_dict:
parameter_series = pd.Series({}, dtype=complex)
else:
parameter_series = pd.Series(parameter_substitution_dict)
parameter_series = parameter_series.add_prefix('unitary_parameter_')
expectation_values = expectation_values.append(parameter_series)
return expectation_values
9 changes: 7 additions & 2 deletions qoqo/measurements/basis_rotation_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def __call__(self, **kwargs) -> pd.Series:
"""
if self.backend is None:
return pd.Series({})
return pd.Series({}, dtype=complex)
# Dict for all read-out registers
output_register_dict: Dict[str, RegisterOutput] = dict()
# Dict for pauli products calculated from each read out register
Expand Down Expand Up @@ -276,4 +276,9 @@ def __call__(self, **kwargs) -> pd.Series:
if 'global_phase' in output_register_dict.keys():
tmp_dict['global_phase'] = output_register_dict['global_phase'].register[0][0]

return pd.Series(tmp_dict, dtype=complex)
if not tmp_dict:
return_series = pd.Series({}, dtype=complex)
else:
return_series = pd.Series(tmp_dict)

return return_series
9 changes: 7 additions & 2 deletions qoqo/measurements/cheated_basis_rotation_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __call__(self, **kwargs) -> pd.Series:
"""
if self.backend is None:
return pd.Series({})
return pd.Series({}, dtype=complex)
# Dict for all read-out registers
output_register_dict: Dict[str, RegisterOutput] = dict()
# Dict for pauli products calculated from each read out register
Expand Down Expand Up @@ -175,4 +175,9 @@ def __call__(self, **kwargs) -> pd.Series:
if 'global_phase' in output_register_dict.keys():
tmp_dict['global_phase'] = output_register_dict['global_phase'].register[0][0]

return pd.Series(tmp_dict)
if not tmp_dict:
return_series = pd.Series({}, dtype=complex)
else:
return_series = pd.Series(tmp_dict)

return return_series
9 changes: 7 additions & 2 deletions qoqo/measurements/cheated_measurement.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __call__(self, include_metadata: bool = False, **kwargs) -> pd.Series:
"""
if self.backend is None:
return pd.Series({})
return pd.Series({}, dtype=complex)
# Dict for all read-out registers
output_register_dict: Dict[str, RegisterOutput] = dict()
# Dict for pauli products calculated from each read out register
Expand Down Expand Up @@ -175,4 +175,9 @@ def __call__(self, include_metadata: bool = False, **kwargs) -> pd.Series:
if 'global_phase' in output_register_dict.keys():
expectation_values['global_phase'] = output_register_dict['global_phase'].register[0][0]

return pd.Series(expectation_values)
if not expectation_values:
return_series = pd.Series({}, dtype=complex)
else:
return_series = pd.Series(expectation_values)

return return_series
30 changes: 15 additions & 15 deletions qoqo/operations/_operations_base_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class Operation():
Qoqo operations can be serialised to json or yaml using the HQS qonfig package.
Operations can be parameterized, where one or more of its internal parameters can be
Operations can be parametrized, where one or more of its internal parameters can be
string expressions instead of standard Python types.
These parameters can be substituted with a substitute_parameters method or by
adding a PragmaParameterSubstitution in the circuit.
Expand Down Expand Up @@ -187,10 +187,10 @@ def to_qonfig(self) -> 'Qonfig[Operation]':
def __init__(self) -> None:
"""Initialize the Operation class"""
self._involved_qubits: Set[Union[int, str]] = set()
self._parameterized = False
self._parametrized = False

@property
def is_parameterized(self) -> bool:
def is_parametrized(self) -> bool:
"""Return True if the operation has symbolic parameters
True if any of the parameters of the gate is a symbol that can be replaced
Expand All @@ -199,7 +199,7 @@ def is_parameterized(self) -> bool:
Returns:
bool
"""
return self._parameterized
return self._parametrized

def substitute_parameters(
self,
Expand Down Expand Up @@ -371,7 +371,7 @@ class GateOperation(Operation):
arguments; init then automatically compares with the default qubit_dict and
parameter_dict of the gate and updates the values that where provided at
initialization. If the keyword arguments have string type, they are
considered symbolic and is_parameterized is true.
considered symbolic and is_parametrized is true.
"""

Expand Down Expand Up @@ -409,18 +409,18 @@ def __init__(self,
arguments; init then automatically compares with the default qubit_dict and
parameter_dict of the gate and updates the values that where provided at
initialization. If the keyword arguments have string type, they are
considered symbolic and is_parameterized is true.
considered symbolic and is_parametrized is true.
"""
self._ordered_qubits_dict = dict()
self._ordered_parameter_dict: Dict[str, CalculatorFloat]
self._ordered_parameter_dict = dict()
if not for_copy:
self._parameterized = False
self._parametrized = False
for key in self._ordered_parameter_dict_default.keys():
self._ordered_parameter_dict[key] = CalculatorFloat(
kwargs.get(key, self._ordered_parameter_dict_default[key]))
if not self._ordered_parameter_dict[key].is_float:
self._parameterized = True
self._parametrized = True
for key in self._ordered_qubits_dict_default.keys():
self._ordered_qubits_dict[key] = cast(
int,
Expand Down Expand Up @@ -486,7 +486,7 @@ def __pow__(self, other: IntoCalculatorFloat) -> 'GateOperation':
(self._ordered_parameter_dict[parameter] * other)
)
if any([not p.is_float for p in return_operation._ordered_parameter_dict.values()]):
return_operation._parameterized = True
return_operation._parametrized = True
return return_operation

def __and__(self, other: object) -> bool:
Expand Down Expand Up @@ -554,7 +554,7 @@ def __copy__(self) -> 'GateOperation':
self_copy = self.__class__(for_copy=True)
self_copy._ordered_qubits_dict = copy(self._ordered_qubits_dict)
self_copy._ordered_parameter_dict = copy(self._ordered_parameter_dict)
self_copy._parameterized = copy(self._parameterized)
self_copy._parametrized = copy(self._parametrized)
self_copy._involved_qubits = copy(self._involved_qubits)
return self_copy

Expand All @@ -580,7 +580,7 @@ def substitute_parameters(self,
Where 'name' is the name of the symbol to be substituted
and new_value is the substituted value
"""
if self.is_parameterized:
if self.is_parametrized:
substitution_string = ''
for key, val in substitution_dict.items():
substitution_string += '{}={}; '.format(key, val)
Expand All @@ -589,7 +589,7 @@ def substitute_parameters(self,
if not parameter.is_float:
new_parameter = parse_string(substitution_string + '; ' + parameter.value)
self._ordered_parameter_dict[key] = CalculatorFloat(new_parameter)
self._parameterized = False
self._parametrized = False

def remap_qubits(self,
mapping_dict: Dict[int, int]) -> None:
Expand Down Expand Up @@ -631,11 +631,11 @@ def unitary_matrix(self) -> np.ndarray:
np.ndarray
Raises:
ValueError: Parameterized gate can not be returned as unitary matrix
ValueError: Parametrized gate can not be returned as unitary matrix
AttributeError: Operation has no unitary matrix method
"""
if self.is_parameterized:
raise ValueError('Parameterized gate can not be returned as unitary matrix')
if self.is_parametrized:
raise ValueError('Parametrized gate can not be returned as unitary matrix')
parameters = {key: val.value for key, val in self._ordered_parameter_dict.items()}
matrix_method = getattr(self, 'unitary_matrix_from_parameters', None)
if matrix_method is None:
Expand Down
2 changes: 1 addition & 1 deletion qoqo/operations/define_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(self,
self._is_input = is_input
self._is_output = is_output
self._involved_qubits = set()
self._parameterized = False
self._parametrized = False

@classmethod
def from_qonfig(cls,
Expand Down
16 changes: 8 additions & 8 deletions qoqo/operations/measurement_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def __init__(self,
readout_index = qubit
self._readout_index = readout_index
self._readout = readout
self._parameterized = False
self._parametrized = False

@classmethod
def from_qonfig(cls,
Expand Down Expand Up @@ -179,7 +179,7 @@ def __init__(self,
self._readout = readout
self._qubit_mapping = qubit_mapping
self._involved_qubits = set(['ALL'])
self._parameterized = False
self._parametrized = False
self._circuit = circuit

@classmethod
Expand Down Expand Up @@ -320,7 +320,7 @@ def __init__(self,
self._readout = readout
self._qubit_mapping = qubit_mapping
self._involved_qubits = set(['ALL'])
self._parameterized = False
self._parametrized = False
self._circuit = circuit

@classmethod
Expand Down Expand Up @@ -457,7 +457,7 @@ def __init__(self,
self._readout = readout
self._qubit_mapping = qubit_mapping
self._involved_qubits = set(['ALL'])
self._parameterized = False
self._parametrized = False

@classmethod
def from_qonfig(cls,
Expand Down Expand Up @@ -569,7 +569,7 @@ def __init__(self,
"""
self._readout = readout
self._circuit = circuit
self._parameterized = False
self._parametrized = False
self._involved_qubits = set(['ALL'])

@classmethod
Expand Down Expand Up @@ -701,7 +701,7 @@ def __init__(self,
self._readout = readout
self._circuit = circuit
self._pauli_product = pauli_product
self._parameterized = False
self._parametrized = False
self._involved_qubits = set(['ALL'])

@classmethod
Expand Down Expand Up @@ -833,7 +833,7 @@ def __init__(self,
self._number_measurements = number_measurements
self._qubit_mapping = qubit_mapping
self._involved_qubits = set(['ALL'])
self._parameterized = False
self._parametrized = False
self._readout = readout

@classmethod
Expand Down Expand Up @@ -1002,7 +1002,7 @@ def __init__(self,
paulis = cast(List[int], list())
self._qubits = qubits
self._involved_qubits = set(self._qubits)
self._parameterized = False
self._parametrized = False
self._paulis = paulis
self._readout = readout
self._readout_index = readout_index
Expand Down
Loading

0 comments on commit b7e185d

Please sign in to comment.