-
Notifications
You must be signed in to change notification settings - Fork 237
Try using regex to change qml to qp #1743
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
Merged
+560
−561
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
539ac9e
Try using regex to change qml to qp
1f18903
Finish changes in tutorial_How_to_optimize_QML_model_using_JAX_cataly…
drdren eda3705
Try using regex to change qml to qp 2
6733002
Fix tutorial_classically_boosted_vqe
drdren 98c7718
Remove extra demos from PR
ff0aa44
Replace qp with qml in tutorial_classically_boosted_vqe
drdren 36b35f0
Merge branch 'master' into dren-regex-qml-qp1
drdren f2d1536
Update dateOfLastModification in metadata
drdren 232202a
Undo changes to gbs
4083b31
Merge branch 'master' into dren-regex-qml-qp1
drdren 80a6c64
Add 'Resource Estimation' to metadata
drdren 849a274
Update category in metadata.json to 'Quantum Machine Learning'
drdren 0bf6131
Update category in metadata.json
drdren 141507f
Update categories in metadata.json
drdren a561bb2
Remove 'Resource Estimation' from categories
drdren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import os | ||
| import re | ||
| import sys | ||
|
|
||
| # Configuration | ||
| LIMIT = 500 | ||
| changes_made = 0 | ||
| modified_dirs = set() # Tracks unique directories that had modifications | ||
|
|
||
| repo_path = './demonstrations_v2' | ||
|
|
||
| # Regex patterns | ||
| import_pattern = re.compile(r'import\s+pennylane\s+as\s+qml\b') | ||
| alias_pattern = re.compile(r'\bqml\.') | ||
|
|
||
| for root, dirs, files in os.walk(repo_path): | ||
| # Sort directories and files in-place so the traversal is alphabetical | ||
| dirs.sort() | ||
| files.sort() | ||
|
|
||
| # Optional: skip hidden directories like .git or virtual environments | ||
| if '.git' in root or 'venv' in root: | ||
| continue | ||
|
|
||
| for file in files: | ||
| if changes_made >= LIMIT: | ||
| break | ||
|
|
||
| if file.endswith('.py'): | ||
| filepath = os.path.join(root, file) | ||
|
|
||
| with open(filepath, 'r', encoding='utf-8') as f: | ||
| lines = f.readlines() | ||
|
|
||
| new_lines = [] | ||
| file_modified = False | ||
|
|
||
| for line in lines: | ||
| original_line = line | ||
|
|
||
| # Apply both regex replacements | ||
| line = import_pattern.sub('import pennylane as qp', line) | ||
| line = alias_pattern.sub('qp.', line) | ||
|
|
||
| new_lines.append(line) | ||
|
|
||
| if original_line != line: | ||
| file_modified = True | ||
| changes_made += 1 | ||
|
|
||
| if changes_made >= LIMIT: | ||
| # If limit reached mid-file, keep the rest of the file unchanged | ||
| new_lines.extend(lines[len(new_lines):]) | ||
| break | ||
|
|
||
| if file_modified: | ||
| with open(filepath, 'w', encoding='utf-8') as f: | ||
|
JerryChen97 marked this conversation as resolved.
Outdated
|
||
| f.writelines(new_lines) | ||
|
|
||
| # Add the current directory to our set of modified directories | ||
| modified_dirs.add(root) | ||
|
|
||
| if changes_made >= LIMIT: | ||
| print(f"Reached limit of {LIMIT} line changes. Stopping.") | ||
| break | ||
|
|
||
| print(f"\nRefactoring complete. Total lines changed: {changes_made}") | ||
|
|
||
| # Print the summary of modified subdirectories | ||
| if modified_dirs: | ||
| print("Files were modified in the following directories:") | ||
| for directory in sorted(modified_dirs): | ||
| print(f" - {directory}") | ||
| else: | ||
| print("No directories were modified (no matches found).") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.