Skip to content
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

style: update linter to use UP and SIM rules #695

Merged
merged 2 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 12 additions & 26 deletions examples/advanced_circuits_algorithms/Grover/Grover.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@
"# Helper function to build C-C-Z gate\n",
"@circuit.subroutine(register=True)\n",
"def ccz(targets=[0, 1, 2]):\n",
" \"\"\"\n",
" implementation of three-qubit gate CCZ\n",
" \"\"\"\n",
" \"\"\"Implementation of three-qubit gate CCZ\"\"\"\n",
" # define three-qubit CCZ gate\n",
" ccz_gate = np.array(\n",
" [\n",
Expand Down Expand Up @@ -264,9 +262,7 @@
"\n",
"# helper function for initialization\n",
"def initialize(n_qubits=3):\n",
" \"\"\"\n",
" function to apply hadamard to all qubits\n",
" \"\"\"\n",
" \"\"\"Function to apply hadamard to all qubits\"\"\"\n",
" # Initialize with superposition\n",
" circ = Circuit()\n",
" circ.h(np.arange(n_qubits))\n",
Expand All @@ -276,9 +272,7 @@
"\n",
"# helper function for phase oracle\n",
"def oracle(item):\n",
" \"\"\"\n",
" function to apply oracle for given target item\n",
" \"\"\"\n",
" \"\"\"Function to apply oracle for given target item\"\"\"\n",
" # instantiate circuit object\n",
" circ = Circuit()\n",
"\n",
Expand All @@ -290,9 +284,7 @@
"\n",
"# helper function for amplification\n",
"def amplify(n_qubits=3):\n",
" \"\"\"\n",
" function for amplitude amplification\n",
" \"\"\"\n",
" \"\"\"Function for amplitude amplification\"\"\"\n",
" # instantiate circuit object\n",
" circ = Circuit()\n",
"\n",
Expand All @@ -306,9 +298,7 @@
"\n",
"# helper function for grover algorithm\n",
"def grover(item, n_qubits=3, n_reps=1):\n",
" \"\"\"\n",
" function to put together individual modules of Grover algorithm\n",
" \"\"\"\n",
" \"\"\"Function to put together individual modules of Grover algorithm\"\"\"\n",
" # initialize\n",
" grover_circ = initialize()\n",
" # oracle and amplify\n",
Expand Down Expand Up @@ -444,7 +434,7 @@
" \"supportedOperations\"\n",
"]\n",
"# Note: This field also exists for other devices like the QPUs\n",
"print(\"Quantum Gates supported by {}:\\n {}\".format(device_name, device_operations))"
"print(f\"Quantum Gates supported by {device_name}:\\n {device_operations}\")"
]
},
{
Expand Down Expand Up @@ -695,7 +685,7 @@
" \"supportedOperations\"\n",
"]\n",
"# Note: This field also exists for other devices like the QPUs\n",
"print(\"Quantum Gates supported by {}:\\n {}\".format(device_name, device_operations))"
"print(f\"Quantum Gates supported by {device_name}:\\n {device_operations}\")"
]
},
{
Expand All @@ -718,9 +708,7 @@
"source": [
"@circuit.subroutine(register=True)\n",
"def CCNot(controls=[0, 1], target=2):\n",
" \"\"\"\n",
" build CCNOT from H, CNOT, T, Ti\n",
" \"\"\"\n",
" \"\"\"Build CCNOT from H, CNOT, T, Ti\"\"\"\n",
" cQb1, cQb2 = controls\n",
" circ = (\n",
" Circuit()\n",
Expand All @@ -745,9 +733,7 @@
"\n",
"\n",
"def CCZ(controls=[0, 1], target=2):\n",
" \"\"\"\n",
" build CCZ from H and CCNOT\n",
" \"\"\"\n",
" \"\"\"Build CCZ from H and CCNOT\"\"\"\n",
" circ = Circuit().h(target).CCNot(controls, target).h(target)\n",
" return circ\n",
"\n",
Expand Down Expand Up @@ -1024,7 +1010,7 @@
" shots = metadata[\"shots\"]\n",
" machine = metadata[\"deviceArn\"]\n",
" # print example metadata\n",
" print(\"{} shots taken on machine {}.\".format(shots, machine))\n",
" print(f\"{shots} shots taken on machine {machine}.\")\n",
"\n",
" # get measurement counts\n",
" counts = results.measurement_counts\n",
Expand Down Expand Up @@ -1091,10 +1077,10 @@
"print(\"Quantum Task Summary\")\n",
"print(t.quantum_tasks_statistics())\n",
"print(\n",
" \"Note: Charges shown are estimates based on your Amazon Braket simulator and quantum processing unit (QPU) task usage. Estimated charges shown may differ from your actual charges. Estimated charges do not factor in any discounts or credits, and you may experience additional charges based on your use of other services such as Amazon Elastic Compute Cloud (Amazon EC2).\"\n",
" \"Note: Charges shown are estimates based on your Amazon Braket simulator and quantum processing unit (QPU) task usage. Estimated charges shown may differ from your actual charges. Estimated charges do not factor in any discounts or credits, and you may experience additional charges based on your use of other services such as Amazon Elastic Compute Cloud (Amazon EC2).\",\n",
")\n",
"print(\n",
" f\"Estimated cost to run this example: {t.qpu_tasks_cost() + t.simulator_tasks_cost():.2f} USD\"\n",
" f\"Estimated cost to run this example: {t.qpu_tasks_cost() + t.simulator_tasks_cost():.2f} USD\",\n",
")"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@
"\n",
"# Print the final amplitude of |11>:\n",
"info = \"Maximum amplified amplitude <110|Final State> after approximately\"\n",
"print(info + \" {} Grover iterations:\\n {}\".format(max_iter, result.values[0]))"
"print(info + f\" {max_iter} Grover iterations:\\n {result.values[0]}\")"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@


def get_unitary(self):
"""
Function to get the unitary matrix corresponding to an entire circuit.
"""Function to get the unitary matrix corresponding to an entire circuit.
Acts on self and returns the corresponding unitary
"""
num_qubits = int(
max(self.qubits) + 1
max(self.qubits) + 1,
) # Coincides with self.qubit_count when qubit indexing is contiguous.

# Define the unitary matrix. Start with the identity matrix.
Expand Down Expand Up @@ -59,7 +58,6 @@ def adjoint(self):
"""Generates a circuit object corresponding to the adjoint of a given circuit, in which the order
of gates is reversed, and each gate is the adjoint (i.e., conjugate transpose) of the original.
"""

adjoint_circ = Circuit()

# Loop through the instructions (gates) in the circuit:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
# helper function to apply XZX to given qubit
@circuit.subroutine(register=True)
def minus_R_B(qubit):
"""
Function to apply a minus sign to |B>|0>. This is achieved by applying XZX to the ancilla qubit.
"""Function to apply a minus sign to |B>|0>. This is achieved by applying XZX to the ancilla qubit.

Args:
qubit: the ancilla qubit on which we apply XZX.

"""
# instantiate circuit object
circ = Circuit()
Expand All @@ -29,15 +29,14 @@ def minus_R_B(qubit):
# Helper function to apply rotation -R0
@circuit.subroutine(register=True)
def minus_R_zero(qubits, use_explicit_unitary=False):
"""
Function to implement transformation: |0,0,...0> -> -|0,0,...0>, all others unchanged.
"""Function to implement transformation: |0,0,...0> -> -|0,0,...0>, all others unchanged.

Args:
qubits: list of qubits on which to apply the gates
use_explicit_unitary (default False): Flag to specify that we could instead implement
the desired gate using a custom gate defined by the unitary diag(-1,1,...,1).
"""

"""
circ = Circuit()

# If the use_explicit_matrix flag is True, we just apply the unitary defined by |0,0,...0> -> -|0,0,...0>
Expand Down Expand Up @@ -92,8 +91,7 @@ def minus_R_zero(qubits, use_explicit_unitary=False):

@circuit.subroutine(register=True)
def grover_iterator(A, flag_qubit, qubits=None, use_explicit_unitary=False):
"""
Function to implement the Grover iterator Q=A R_0 A* R_B.
"""Function to implement the Grover iterator Q=A R_0 A* R_B.

Args:
A: Circuit defining the unitary A
Expand All @@ -103,22 +101,19 @@ def grover_iterator(A, flag_qubit, qubits=None, use_explicit_unitary=False):
If qubits is different from A.qubits, A is applied to qubits instead.
use_explicit_unitary: Flag to specify that we should implement R_0 using using a custom
gate defined by the unitary diag(-1,1,...,1). Default is False.

"""
# If no qubits are passed, apply the gates to the targets of A
if qubits is None:
qubits = A.qubits
else:
# If qubits are passed, make sure it's the right number to remap from A.
if len(qubits) != len(A.qubits):
raise ValueError(
"Number of desired target qubits differs from number of targets in A".format()
)
elif len(qubits) != len(A.qubits):
raise ValueError(
"Number of desired target qubits differs from number of targets in A".format(),
)

# Verify that flag_qubit is one of the qubits on which A acts, or one of the user defined qubits
if flag_qubit not in qubits:
raise ValueError(
"flag_qubit {flag_qubit} is not in targets of A".format(flag_qubit=repr(flag_qubit))
)
raise ValueError(f"flag_qubit {flag_qubit!r} is not in targets of A")

# Instantiate the circuit
circ = Circuit()
Expand All @@ -140,8 +135,7 @@ def grover_iterator(A, flag_qubit, qubits=None, use_explicit_unitary=False):

@circuit.subroutine(register=True)
def qaa(A, flag_qubit, num_iterations, qubits=None, use_explicit_unitary=False):
"""
Function to implement the Quantum Amplitude Amplification Q^m, where Q=A R_0 A* R_B, m=num_iterations.
"""Function to implement the Quantum Amplitude Amplification Q^m, where Q=A R_0 A* R_B, m=num_iterations.

Args:
A: Circuit defining the unitary A
Expand All @@ -152,6 +146,7 @@ def qaa(A, flag_qubit, num_iterations, qubits=None, use_explicit_unitary=False):
If qubits is different from A.qubits, A is applied to qubits instead.
use_explicit_unitary: Flag to specify that we should implement R_0 using using a custom
gate defined by the unitary diag(-1,1,...,1). Default is False.

"""
# Instantiate the circuit
circ = Circuit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,44 +241,42 @@
"source": [
"# qft subroutine without swaps\n",
"def qft_no_swap(qubits):\n",
" \"\"\"\n",
" Subroutine of the QFT excluding the final SWAP gates, applied to the qubits argument.\n",
" \"\"\"Subroutine of the QFT excluding the final SWAP gates, applied to the qubits argument.\n",
" Returns the a circuit object.\n",
"\n",
" Args:\n",
" qubits (int): The list of qubits on which to apply the QFT\n",
" \"\"\"\n",
"\n",
" \"\"\"\n",
" # On a single qubit, the QFT is just a Hadamard.\n",
" if len(qubits) == 1:\n",
" return Circuit().h(qubits)\n",
"\n",
" # For more than one qubit, we define the QFT recursively (as shown on the right half of the image above):\n",
" else:\n",
" qftcirc = Circuit()\n",
" qftcirc = Circuit()\n",
"\n",
" # First add a Hadamard gate\n",
" qftcirc.h(qubits[0])\n",
" # First add a Hadamard gate\n",
" qftcirc.h(qubits[0])\n",
"\n",
" # Then apply the controlled rotations, with weights (angles) defined by the distance to the control qubit.\n",
" for k, qubit in enumerate(qubits[1:]):\n",
" qftcirc.cphaseshift(qubit, qubits[0], 2 * math.pi / (2 ** (k + 2)))\n",
" # Then apply the controlled rotations, with weights (angles) defined by the distance to the control qubit.\n",
" for k, qubit in enumerate(qubits[1:]):\n",
" qftcirc.cphaseshift(qubit, qubits[0], 2 * math.pi / (2 ** (k + 2)))\n",
"\n",
" # Now apply the above gates recursively to the rest of the qubits\n",
" qftcirc.add(qft_no_swap(qubits[1:]))\n",
" # Now apply the above gates recursively to the rest of the qubits\n",
" qftcirc.add(qft_no_swap(qubits[1:]))\n",
"\n",
" return qftcirc\n",
"\n",
"\n",
"# To complete the full QFT, add swap gates to reverse the order of the qubits\n",
"@circuit.subroutine(register=True)\n",
"def qft_recursive(qubits):\n",
" \"\"\"\n",
" Construct a circuit object corresponding to the Quantum Fourier Transform (QFT)\n",
" \"\"\"Construct a circuit object corresponding to the Quantum Fourier Transform (QFT)\n",
" algorithm, applied to the argument qubits.\n",
"\n",
" Args:\n",
" qubits (int): The list of qubits on which to apply the QFT\n",
"\n",
" \"\"\"\n",
" qftcirc = Circuit()\n",
"\n",
Expand Down Expand Up @@ -312,12 +310,12 @@
"source": [
"@circuit.subroutine(register=True)\n",
"def qft(qubits):\n",
" \"\"\"\n",
" Construct a circuit object corresponding to the Quantum Fourier Transform (QFT)\n",
" \"\"\"Construct a circuit object corresponding to the Quantum Fourier Transform (QFT)\n",
" algorithm, applied to the argument qubits. Does not use recursion to generate the QFT.\n",
"\n",
" Args:\n",
" qubits (int): The list of qubits on which to apply the QFT\n",
"\n",
" \"\"\"\n",
" qftcirc = Circuit()\n",
"\n",
Expand Down Expand Up @@ -361,12 +359,12 @@
"source": [
"@circuit.subroutine(register=True)\n",
"def inverse_qft(qubits):\n",
" \"\"\"\n",
" Construct a circuit object corresponding to the inverse Quantum Fourier Transform (QFT)\n",
" \"\"\"Construct a circuit object corresponding to the inverse Quantum Fourier Transform (QFT)\n",
" algorithm, applied to the argument qubits. Does not use recursion to generate the circuit.\n",
"\n",
" Args:\n",
" qubits (int): The list of qubits on which to apply the inverse QFT\n",
"\n",
" \"\"\"\n",
" # instantiate circuit object\n",
" qftcirc = Circuit()\n",
Expand Down Expand Up @@ -459,7 +457,7 @@
"print(my_qft_circ)\n",
"\n",
"# show inverse QFT example circuit\n",
"print(\"\")\n",
"print()\n",
"print(\"INVERSE-QFT CIRCUIT:\")\n",
"my_iqft_circ = inverse_qft(qubits)\n",
"print(my_iqft_circ)"
Expand Down Expand Up @@ -519,7 +517,7 @@
"print(my_qft_circ)\n",
"\n",
"# show inverse QFT example circuit\n",
"print(\"\")\n",
"print()\n",
"print(\"INVERSE-QFT CIRCUIT:\")\n",
"my_iqft_circ = inverse_qft(qubits)\n",
"print(my_iqft_circ)"
Expand Down Expand Up @@ -704,7 +702,7 @@
"circ.inverse_qft(qubits)\n",
"\n",
"# print circuit including QFT\n",
"print(\"\")\n",
"print()\n",
"print(\"2. Full circuit including inverse QFT:\")\n",
"print(circ)"
]
Expand Down Expand Up @@ -845,7 +843,7 @@
"circ.inverse_qft(qubits)\n",
"\n",
"# print circuit including QFT\n",
"print(\"\")\n",
"print()\n",
"print(\"2. Full circuit including inverse QFT:\")\n",
"print(circ)\n",
"\n",
Expand Down Expand Up @@ -1105,10 +1103,10 @@
"# print circuits\n",
"print(\"Circuit for QFT (using non-recursive implementation):\")\n",
"print(qft(qubits))\n",
"print(\"\")\n",
"print()\n",
"print(\"Circuit for QFT (using recursive implementation):\")\n",
"print(qft_recursive(qubits))\n",
"print(\"\")\n",
"print()\n",
"print(\"Circuit for inverse QFT:\")\n",
"print(inverse_qft(qubits))"
]
Expand Down
Loading