-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sourcery refactored main branch #20
base: main
Are you sure you want to change the base?
Conversation
gamma = Parameter('g{}_{}_{}'.format(p, n1, n2)) | ||
gamma = Parameter(f'g{p}_{n1}_{n2}') | ||
qc.cx(n1, n2) | ||
qc.rz(gamma, n2) | ||
qc.cx(n1, n2) | ||
beta = Parameter('b{}'.format(p)) | ||
beta = Parameter(f'b{p}') | ||
for node in self.zz_graph.nodes(): | ||
qc.rx(beta,node) | ||
qc.barrier() | ||
qc.measure(range(n), range(n)) | ||
|
||
trans_ckt = self.__compile_with_backend(ckt_qiskit = qc) | ||
filename = 'uncompiled_' + self.output_file_name | ||
filename = f'uncompiled_{self.output_file_name}' | ||
qc.qasm(filename = filename) | ||
self.__fix_param_names(filename) | ||
filename = 'naive_compiled_' + self.output_file_name | ||
filename = f'naive_compiled_{self.output_file_name}' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CompileQAOAQiskit.__naive_compilation
refactored with the following changes:
- Replace call to format with f-string [×2] (
use-fstring-for-formatting
) - Use f-string instead of string concatenation [×2] (
use-fstring-for-concatenation
)
assert self.Backend in self.supported_backends | ||
else: | ||
self.Backend = 'qiskit' | ||
assert self.Backend in self.supported_backends | ||
|
||
assert self.Backend in self.supported_backends |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CompileQAOAQiskit.__load_config
refactored with the following changes:
- Hoist repeated code outside conditional statement (
hoist-statement-from-if
)
fmap[logical_q[0:]] = int(physical_q[0:]) | ||
fmap[logical_q[:]] = int(physical_q[:]) | ||
|
||
final_map = [ | ||
fmap[str(i)] | ||
for i in range( | ||
qiskit_ckt_object.width() - qiskit_ckt_object.num_qubits | ||
) | ||
] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CompileQAOAQiskit.__final_mapping_ic
refactored with the following changes:
- Replace a[0:x] with a[:x] and a[x:len(a)] with a[x:] [×2] (
remove-redundant-slice-index
) - Convert for loop into list comprehension (
list-comprehension
)
if not self.incr_c_var_awareness: | ||
swap_dist = self.qq_distances[_physical_q1][_physical_q2] | ||
else: | ||
swap_dist = self.noise_aware_qq_distances[_physical_q1][_physical_q2] | ||
swap_dist = ( | ||
self.noise_aware_qq_distances[_physical_q1][_physical_q2] | ||
if self.incr_c_var_awareness | ||
else self.qq_distances[_physical_q1][_physical_q2] | ||
) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CompileQAOAQiskit.__sort_zz_by_qq_distances
refactored with the following changes:
- Swap if/else branches of if expression to remove negation (
swap-if-expression
) - Replace if statement with if expression (
assign-if-exp
)
gamma = Parameter('g{}_{}_{}'.format(p, n1, n2)) | ||
gamma = Parameter(f'g{p}_{n1}_{n2}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CompileQAOAQiskit.__construct_single_layer_ckt_ic
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
out = open('{}_fixed'.format(filename), 'w') | ||
for line in f: | ||
captures = re.search('(g\d+)_(\d+)_(\d+)', line) | ||
if captures: | ||
captures = captures.groups() | ||
g = captures[0] | ||
n1 = captures[1] | ||
n2 = captures[2] | ||
if '({}, {})'.format(n1, n2) in all_keys: | ||
coeff = 2*float(self.zz_dict['({}, {})'.format(n1, n2)]) | ||
else: | ||
coeff = 2*float(self.zz_dict['({}, {})'.format(n2, n1)]) | ||
line = line.replace('{}_{}_{}'.format(g, n1, n2), str(coeff) + '*' + g) | ||
out.write(line) | ||
out.close() | ||
with open(f'{filename}_fixed', 'w') as out: | ||
for line in f: | ||
if captures := re.search('(g\d+)_(\d+)_(\d+)', line): | ||
captures = captures.groups() | ||
g = captures[0] | ||
n1 = captures[1] | ||
n2 = captures[2] | ||
if f'({n1}, {n2})' in all_keys: | ||
coeff = 2 * float(self.zz_dict[f'({n1}, {n2})']) | ||
else: | ||
coeff = 2 * float(self.zz_dict[f'({n2}, {n1})']) | ||
line = line.replace(f'{g}_{n1}_{n2}', f'{str(coeff)}*{g}') | ||
out.write(line) | ||
os.remove(filename) | ||
os.rename(filename + '_fixed', filename) | ||
os.rename(f'{filename}_fixed', filename) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function CompileQAOAQiskit.__fix_param_names
refactored with the following changes:
- Use
with
when opening file to ensure closure (ensure-file-closed
) - Replace call to format with f-string [×5] (
use-fstring-for-formatting
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Use f-string instead of string concatenation [×3] (
use-fstring-for-concatenation
)
logical_to_physical_layout = {} | ||
for node in problem_zz_interactions_graph.nodes(): | ||
logical_to_physical_layout[node] = 'NA' | ||
logical_to_physical_layout = { | ||
node: 'NA' for node in problem_zz_interactions_graph.nodes() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function initial_layout
refactored with the following changes:
- Convert for loop into dictionary comprehension (
dict-comprehension
) - Use named expression to simplify assignment and conditional (
use-named-expression
) - Convert for loop into list comprehension (
list-comprehension
) - Swap if/else branches (
swap-if-else-branches
) - Replaces an empty collection equality with a boolean operation [×3] (
simplify-empty-collection-comparison
) - Replace a for append loop with list extend (
for-append-to-extend
)
This removes the following comments ( why? ):
#if some logical neighbors are already placed find unallocated physical neighbors
#assign best available physical qubit to logical
#qubit if none of the logical neighbors are placed
#check which neighbors are already placced
if sorted_program_qubits == []: | ||
if not sorted_program_qubits: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function sort_program_qubits
refactored with the following changes:
- Replaces an empty collection equality with a boolean operation (
simplify-empty-collection-comparison
)
print('Unconnected node: {}'.format(node)) | ||
print(f'Unconnected node: {node}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function problem_profiling
refactored with the following changes:
- Replace call to format with f-string (
use-fstring-for-formatting
)
strength = 0 | ||
for neigh in neighbours: | ||
strength += coupling_graph[node][neigh]['weight'] | ||
strength = sum(coupling_graph[node][neigh]['weight'] for neigh in neighbours) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function hardware_qubit_strength_vqp
refactored with the following changes:
- Convert for loop into call to sum() (
sum-comprehension
)
Branch
main
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
main
branch, then run:Help us improve this pull request!