Skip to content

Commit

Permalink
various documentation fixes (#4059)
Browse files Browse the repository at this point in the history
  • Loading branch information
albi3ro committed Apr 28, 2023
1 parent f7e7289 commit 6410889
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 50 deletions.
4 changes: 2 additions & 2 deletions doc/code/qml_pauli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ groupings according to a binary relation (e.g., qubit-wise commuting):
>>> observables = [qml.PauliY(0), qml.PauliX(0) @ qml.PauliX(1), qml.PauliZ(1)]
>>> obs_groupings = group_observables(observables)
>>> obs_groupings
[[Tensor(PauliX(wires=[0]), PauliX(wires=[1]))],
[[PauliX(wires=[0]) @ PauliX(wires=[1])],
[PauliY(wires=[0]), PauliZ(wires=[1])]]

The :math:`C_{J}` coefficients for each :math:`P_J` Pauli word making up a
Expand All @@ -111,7 +111,7 @@ the groups of observables:
>>> coeffs = [1.43, 4.21, 0.97]
>>> obs_groupings, coeffs_groupings = group_observables(obs, coeffs, 'qwc', 'rlf')
>>> obs_groupings
[[Tensor(PauliX(wires=[0]), PauliX(wires=[1]))],
[[PauliX(wires=[0]) @ PauliX(wires=[1])],
[PauliY(wires=[0]), PauliZ(wires=[1])]]
>>> coeffs_groupings
[[4.21], [1.43, 0.97]]
25 changes: 15 additions & 10 deletions pennylane/gradients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def circuit(weights):
Comparing this to autodifferentiation:
>>> qml.grad(circuit)(weights)
>>> qml.jacobian(circuit)(weights)
array([[-0.04673668, -0.09442394, -0.14409127],
[ 0.04673668, 0.09442394, 0.14409127]])
Expand All @@ -176,8 +176,9 @@ def decorated_circuit(weights):
return qml.probs(wires=1)
>>> decorated_circuit(weights)
tensor([[-0.04673668, -0.09442394, -0.14409127],
[ 0.04673668, 0.09442394, 0.14409127]], requires_grad=True)
(tensor([-0.04673668, 0.04673668], requires_grad=True),
tensor([-0.09442394, 0.09442394], requires_grad=True),
tensor([-0.14409127, 0.14409127], requires_grad=True))
.. note::
Expand Down Expand Up @@ -217,11 +218,15 @@ def circuit(weights):
>>> circuit(weights)
tensor(0.9316158, requires_grad=True)
>>> qml.gradients.param_shift(circuit)(weights) # gradient
array([[-0.09347337, -0.18884787, -0.28818254]])
>>> qml.jacobian(qml.gradients.param_shift(circuit))(weights) # hessian
array([[[-0.9316158 , 0.01894799, 0.0289147 ],
[ 0.01894799, -0.9316158 , 0.05841749],
[ 0.0289147 , 0.05841749, -0.9316158 ]]])
(tensor(-0.09347337, requires_grad=True),
tensor(-0.18884787, requires_grad=True),
tensor(-0.28818254, requires_grad=True))
>>> def stacked_output(weights):
... return qml.numpy.stack(qml.gradients.param_shift(circuit)(weights))
>>> qml.jacobian(stacked_output)(weights) # hessian
array([[-0.9316158 , 0.01894799, 0.0289147 ],
[ 0.01894799, -0.9316158 , 0.05841749],
[ 0.0289147 , 0.05841749, -0.9316158 ]])
Another way to compute higher-order derivatives is by passing the ``max_diff`` and
``diff_method`` arguments to the QNode and by successive differentiation:
Expand Down Expand Up @@ -256,7 +261,7 @@ def circuit(weights):
weights = np.array([0.1, 0.2, 0.3], requires_grad=True)
with qml.tape.JacobianTape() as tape:
with qml.tape.QuantumTape() as tape:
qml.RX(weights[0], wires=0)
qml.RY(weights[1], wires=1)
qml.CNOT(wires=[0, 1])
Expand Down Expand Up @@ -285,7 +290,7 @@ def circuit(weights):
>>> dev = qml.device("default.qubit", wires=2)
>>> fn(qml.execute(gradient_tapes, dev, None))
[[-0.09347337 -0.18884787 -0.28818254]]
(array(-0.09347337), array(-0.18884787), array(-0.28818254))
Note that the post-processing function ``fn`` returned by the
gradient transform is applied to the flat list of results returned
Expand Down
4 changes: 2 additions & 2 deletions pennylane/transforms/batch_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def batch_params(tape, all_operations=False):
dev = qml.device("default.qubit", wires=3)
@qml.batch_params
@qml.beta.qnode(dev)
@qml.qnode(dev)
def circuit(x, weights):
qml.RX(x, wires=0)
qml.RY(0.2, wires=1)
Expand Down Expand Up @@ -111,7 +111,7 @@ def circuit(x, weights):
.. code-block:: python
@qml.batch_params(all_operations=True)
@qml.beta.qnode(dev)
@qml.qnode(dev)
def circuit(x, weights):
qml.RX(x, wires=0)
qml.RY([0.2, 0.2, 0.2], wires=1)
Expand Down
4 changes: 2 additions & 2 deletions pennylane/transforms/decompositions/single_qubit_unitary.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ def xyx_decomposition(U, wire, return_global_phase=False):
element of the returned list of operations.
Returns:
list[qml.Operation]: Returns a list of of gates, an ``RX``, an ``RY`` and
list[Operation]: Returns a list of of gates, an ``RX``, an ``RY`` and
another ``RX`` gate, which when applied in the order of appearance in the list is equivalent
to the unitary :math:`U` up to global phase. If `return_global_phase=True`,
the global phase is returned as the last element of the list.
**Example**
>>> U = np.array([[-0.28829348-0.78829734j, 0.30364367+0.45085995j],
[ 0.53396245-0.10177564j, 0.76279558-0.35024096j]])
... [ 0.53396245-0.10177564j, 0.76279558-0.35024096j]])
>>> decomp = xyx_decomposition(U, 0, return_global_phase=True)
>>> decomp
[RX(array(0.45246584), wires=[0]),
Expand Down
4 changes: 2 additions & 2 deletions pennylane/transforms/defer_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ def qfunc(par):
>>> qnode = qml.QNode(transformed_qfunc, dev)
>>> par = np.array(np.pi/2, requires_grad=True)
>>> qnode(par)
tensor(-0.12269009, requires_grad=True)
tensor(0.43487747, requires_grad=True)
We can also differentiate parameters passed to conditional operations:
>>> qml.grad(qnode)(par)
-0.9924450321351936
tensor(-0.49622252, requires_grad=True)
"""
measured_wires = {}

Expand Down
4 changes: 2 additions & 2 deletions pennylane/transforms/optimization/pattern_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ def circuit():
optimizing the circuit. The number of CNOTs in the circuit is reduced by one.
>>> qml.specs(qnode)()["gate_types"]["CNOT"]
3
4
>>> qml.specs(optimized_qnode)()["gate_types"]["CNOT"]
2
3
>>> print(qml.draw(qnode)())
0: ─╭X──────────╭X────┤ <X>
Expand Down
2 changes: 1 addition & 1 deletion pennylane/transforms/optimization/undo_swaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def qfunc():
>>> optimized_qfunc = undo_swaps(qfunc)
>>> optimized_qnode = qml.QNode(optimized_qfunc, dev)
>>> print(qml.draw(optimized_qnode)(1, 2))
>>> print(qml.draw(optimized_qnode)())
0: ──Y──┤ ⟨Z⟩
1: ──H──┤
2: ──X──┤
Expand Down
7 changes: 4 additions & 3 deletions pennylane/transforms/qmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,8 @@ def qmc():
algorithm
>>> qml.specs(qmc, expansion_strategy="device")()
{'gate_sizes': defaultdict(int, {1: 15943, 2: 15812, 7: 126, 6: 1}),
{'resources': Resources(num_wires=12, num_gates=31882, gate_types=defaultdict(<class 'int'>, {'RY': 7747, 'CNOT': 7874, 'Hadamard': 258, 'CZ': 126, 'Adjoint(CNOT)': 7812, 'Adjoint(RY)': 7686, 'PauliX': 252, 'MultiControlledX': 126, 'Adjoint(QFT)': 1}), depth=30610, shots=0),
'gate_sizes': defaultdict(int, {1: 15943, 2: 15812, 7: 126, 6: 1}),
'gate_types': defaultdict(int,
{'RY': 7747,
'CNOT': 7874,
Expand All @@ -320,13 +321,13 @@ def qmc():
'num_observables': 1,
'num_diagonalizing_gates': 0,
'num_used_wires': 12,
'depth': 30610,
'num_trainable_params': 15433,
'depth': 30610,
'num_device_wires': 12,
'device_name': 'default.qubit.autograd',
'expansion_strategy': 'gradient',
'gradient_options': {},
'interface': 'autograd',
'interface': 'auto',
'diff_method': 'best',
'gradient_fn': 'backprop'}
"""
Expand Down
35 changes: 18 additions & 17 deletions pennylane/transforms/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def specs(qnode, max_expansion=None, expansion_strategy=None):
x = np.array([0.1, 0.2])
dev = qml.device('default.qubit', wires=2)
@qml.qnode(dev, diff_method="parameter-shift", shift=np.pi / 4)
@qml.qnode(dev, diff_method="parameter-shift", shifts=np.pi / 4)
def circuit(x, add_ry=True):
qml.RX(x[0], wires=0)
qml.CNOT(wires=(0,1))
Expand All @@ -63,22 +63,23 @@ def circuit(x, add_ry=True):
return qml.probs(wires=(0,1))
>>> qml.specs(circuit)(x, add_ry=False)
{'gate_sizes': defaultdict(int, {1: 1, 2: 1}),
'gate_types': defaultdict(int, {'RX': 1, 'CNOT': 1}),
'num_operations': 2,
'num_observables': 1,
'num_diagonalizing_gates': 0,
'num_used_wires': 2,
'depth': 2,
'num_trainable_params': 1,
'num_device_wires': 2,
'device_name': 'default.qubit',
'diff_method': 'parameter-shift',
'expansion_strategy': 'gradient',
'gradient_options': {'shift': 0.7853981633974483},
'interface': 'autograd',
'gradient_fn': 'pennylane.gradients.parameter_shift.param_shift',
'num_gradient_executions': 2}
{'resources': Resources(num_wires=2, num_gates=2, gate_types=defaultdict(<class 'int'>, {'RX': 1, 'CNOT': 1}), depth=2, shots=0),
'gate_sizes': defaultdict(int, {1: 1, 2: 1}),
'gate_types': defaultdict(int, {'RX': 1, 'CNOT': 1}),
'num_operations': 2,
'num_observables': 1,
'num_diagonalizing_gates': 0,
'num_used_wires': 2,
'num_trainable_params': 1,
'depth': 2,
'num_device_wires': 2,
'device_name': 'default.qubit',
'expansion_strategy': 'gradient',
'gradient_options': {'shifts': 0.7853981633974483},
'interface': 'auto',
'diff_method': 'parameter-shift',
'gradient_fn': 'pennylane.gradients.parameter_shift.param_shift',
'num_gradient_executions': 2}
"""

Expand Down
18 changes: 10 additions & 8 deletions pennylane/transforms/split_non_commuting.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def split_non_commuting(tape):
.. code-block:: python3
dev = qml.device("default.qubit", wires=1)
dev = qml.device("default.qubit", wires=2)
@qml.transforms.split_non_commuting
@qml.qnode(dev)
Expand Down Expand Up @@ -104,7 +104,10 @@ def circuit0(x):
:math:`(\langle \sigma_x^0 \rangle, \langle \sigma_z^0 \rangle, \langle \sigma_y^1 \rangle, \langle \sigma_z^0\sigma_z^1 \rangle)`.
>>> circuit0([np.pi/4, np.pi/4])
tensor([0.70710678, 0.5 , 0. , 0.5 ], requires_grad=True)
(tensor(0.70710678, requires_grad=True),
tensor(0.5, requires_grad=True),
tensor(0., requires_grad=True),
tensor(0.5, requires_grad=True))
.. details::
Expand Down Expand Up @@ -139,12 +142,11 @@ def circuit0(x):
In this example, the groupings are ``group_coeffs = [[0,2], [1,3]]`` and ``processing_fn`` makes sure that the final output is of the same shape and ordering:
>>> processing_fn(tapes)
tensor([tensor(expval(PauliZ(wires=[0]) @ PauliZ(wires=[1])), dtype=object, requires_grad=True),
tensor(expval(PauliX(wires=[0]) @ PauliX(wires=[1])), dtype=object, requires_grad=True),
tensor(expval(PauliZ(wires=[0])), dtype=object, requires_grad=True),
tensor(expval(PauliX(wires=[0])), dtype=object, requires_grad=True)],
dtype=object, requires_grad=True)
>>> processing_fn([t.measurements for t in tapes])
(expval(PauliZ(wires=[0]) @ PauliZ(wires=[1])),
expval(PauliX(wires=[0]) @ PauliX(wires=[1])),
expval(PauliZ(wires=[0])),
expval(PauliX(wires=[0])))
"""

Expand Down
2 changes: 1 addition & 1 deletion pennylane/transforms/zx/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def mod_5_4():
qml.apply(g)
return qml.expval(qml.PauliZ(wires=0))
>>> mod_5_4_opt()
>>> mod_5_4()
-0.9999999999999989
.. note::
Expand Down

0 comments on commit 6410889

Please sign in to comment.