Skip to content

Commit f1b2580

Browse files
Doc fix ZZ_feature_maps (Qiskit#13231)
* initial formatting changes * .compose used for concatination of two circuits * updated docstring data property * updated entanglement_blocks docstring * updated docstring of num_parameters * updates docstring of parameters * update _zz_feature_map.py documentation * update changes to ZZ_Feature_Map * update doc strings matched with parent class * _zz_feature_map.py reformatted
1 parent 54139be commit f1b2580

File tree

3 files changed

+122
-31
lines changed

3 files changed

+122
-31
lines changed

qiskit/circuit/library/blueprintcircuit.py

+64
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def qregs(self, qregs):
9494

9595
@property
9696
def data(self):
97+
"""The circuit data (instructions and context).
98+
99+
Returns:
100+
QuantumCircuitData: a list-like object containing the :class:`.CircuitInstruction`\\ s
101+
for each instruction.
102+
"""
97103
if not self._is_built:
98104
self._build()
99105
return super().data
@@ -110,12 +116,70 @@ def draw(self, *args, **kwargs):
110116

111117
@property
112118
def num_parameters(self) -> int:
119+
"""The number of parameter objects in the circuit."""
113120
if not self._is_built:
114121
self._build()
115122
return super().num_parameters
116123

117124
@property
118125
def parameters(self) -> ParameterView:
126+
"""The parameters defined in the circuit.
127+
128+
This attribute returns the :class:`.Parameter` objects in the circuit sorted
129+
alphabetically. Note that parameters instantiated with a :class:`.ParameterVector`
130+
are still sorted numerically.
131+
132+
Examples:
133+
134+
The snippet below shows that insertion order of parameters does not matter.
135+
136+
.. code-block:: python
137+
138+
>>> from qiskit.circuit import QuantumCircuit, Parameter
139+
>>> a, b, elephant = Parameter("a"), Parameter("b"), Parameter("elephant")
140+
>>> circuit = QuantumCircuit(1)
141+
>>> circuit.rx(b, 0)
142+
>>> circuit.rz(elephant, 0)
143+
>>> circuit.ry(a, 0)
144+
>>> circuit.parameters # sorted alphabetically!
145+
ParameterView([Parameter(a), Parameter(b), Parameter(elephant)])
146+
147+
Bear in mind that alphabetical sorting might be unintuitive when it comes to numbers.
148+
The literal "10" comes before "2" in strict alphabetical sorting.
149+
150+
.. code-block:: python
151+
152+
>>> from qiskit.circuit import QuantumCircuit, Parameter
153+
>>> angles = [Parameter("angle_1"), Parameter("angle_2"), Parameter("angle_10")]
154+
>>> circuit = QuantumCircuit(1)
155+
>>> circuit.u(*angles, 0)
156+
>>> circuit.draw()
157+
┌─────────────────────────────┐
158+
q: ┤ U(angle_1,angle_2,angle_10) ├
159+
└─────────────────────────────┘
160+
>>> circuit.parameters
161+
ParameterView([Parameter(angle_1), Parameter(angle_10), Parameter(angle_2)])
162+
163+
To respect numerical sorting, a :class:`.ParameterVector` can be used.
164+
165+
.. code-block:: python
166+
167+
>>> from qiskit.circuit import QuantumCircuit, Parameter, ParameterVector
168+
>>> x = ParameterVector("x", 12)
169+
>>> circuit = QuantumCircuit(1)
170+
>>> for x_i in x:
171+
... circuit.rx(x_i, 0)
172+
>>> circuit.parameters
173+
ParameterView([
174+
ParameterVectorElement(x[0]), ParameterVectorElement(x[1]),
175+
ParameterVectorElement(x[2]), ParameterVectorElement(x[3]),
176+
..., ParameterVectorElement(x[11])
177+
])
178+
179+
180+
Returns:
181+
The sorted :class:`.Parameter` objects in the circuit.
182+
"""
119183
if not self._is_built:
120184
self._build()
121185
return super().parameters

qiskit/circuit/library/data_preparation/_zz_feature_map.py

+53-31
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,60 @@ class ZZFeatureMap(PauliFeatureMap):
3838
3939
Examples:
4040
41-
>>> from qiskit.circuit.library import ZZFeatureMap
42-
>>> prep = ZZFeatureMap(2, reps=1)
43-
>>> print(prep.decompose())
44-
┌───┐┌─────────────┐
45-
q_0: ┤ H ├┤ P(2.0*x[0]) ├──■──────────────────────────────────────■──
46-
├───┤├─────────────┤┌─┴─┐┌────────────────────────────────┐┌─┴─┐
47-
q_1: ┤ H ├┤ P(2.0*x[1]) ├┤ X ├┤ P(2.0*(pi - x[0])*(pi - x[1])) ├┤ X ├
48-
└───┘└─────────────┘└───┘└────────────────────────────────┘└───┘
49-
50-
>>> from qiskit.circuit.library import EfficientSU2
51-
>>> classifier = ZZFeatureMap(3) + EfficientSU2(3)
52-
>>> classifier.num_parameters
53-
15
54-
>>> classifier.parameters # 'x' for the data preparation, 'θ' for the SU2 parameters
55-
ParameterView([
56-
ParameterVectorElement(x[0]), ParameterVectorElement(x[1]),
57-
ParameterVectorElement(x[2]), ParameterVectorElement(θ[0]),
58-
ParameterVectorElement(θ[1]), ParameterVectorElement(θ[2]),
59-
ParameterVectorElement(θ[3]), ParameterVectorElement(θ[4]),
60-
ParameterVectorElement(θ[5]), ParameterVectorElement(θ[6]),
61-
ParameterVectorElement(θ[7]), ParameterVectorElement(θ[8]),
62-
ParameterVectorElement(θ[9]), ParameterVectorElement(θ[10]),
63-
ParameterVectorElement(θ[11]), ParameterVectorElement(θ[12]),
64-
ParameterVectorElement(θ[13]), ParameterVectorElement(θ[14]),
65-
ParameterVectorElement(θ[15]), ParameterVectorElement(θ[16]),
66-
ParameterVectorElement(θ[17]), ParameterVectorElement(θ[18]),
67-
ParameterVectorElement(θ[19]), ParameterVectorElement(θ[20]),
68-
ParameterVectorElement(θ[21]), ParameterVectorElement(θ[22]),
69-
ParameterVectorElement(θ[23])
70-
])
71-
>>> classifier.count_ops()
41+
.. code-block::
42+
43+
from qiskit.circuit.library import ZZFeatureMap
44+
prep = ZZFeatureMap(2, reps=1)
45+
print(prep.decompose())
46+
47+
.. parsed-literal::
48+
┌───┐┌─────────────┐
49+
q_0: ┤ H ├┤ P(2.0*x[0]) ├──■──────────────────────────────────────■──
50+
├───┤├─────────────┤┌─┴─┐┌────────────────────────────────┐┌─┴─┐
51+
q_1: ┤ H ├┤ P(2.0*x[1]) ├┤ X ├┤ P(2.0*(pi - x[0])*(pi - x[1])) ├┤ X ├
52+
└───┘└─────────────┘└───┘└────────────────────────────────┘└───┘
53+
54+
.. code-block::
55+
56+
from qiskit.circuit.library import EfficientSU2
57+
classifier = ZZFeatureMap(3).compose(EfficientSU2(3))
58+
classifier.num_parameters
59+
60+
.. parsed-literal::
61+
62+
27
63+
64+
.. code-block::
65+
66+
classifier.parameters # 'x' for the data preparation, 'θ' for the SU2 parameters
67+
68+
.. parsed-literal::
69+
70+
ParameterView([
71+
ParameterVectorElement(x[0]), ParameterVectorElement(x[1]),
72+
ParameterVectorElement(x[2]), ParameterVectorElement(θ[0]),
73+
ParameterVectorElement(θ[1]), ParameterVectorElement(θ[2]),
74+
ParameterVectorElement(θ[3]), ParameterVectorElement(θ[4]),
75+
ParameterVectorElement(θ[5]), ParameterVectorElement(θ[6]),
76+
ParameterVectorElement(θ[7]), ParameterVectorElement(θ[8]),
77+
ParameterVectorElement(θ[9]), ParameterVectorElement(θ[10]),
78+
ParameterVectorElement(θ[11]), ParameterVectorElement(θ[12]),
79+
ParameterVectorElement(θ[13]), ParameterVectorElement(θ[14]),
80+
ParameterVectorElement(θ[15]), ParameterVectorElement(θ[16]),
81+
ParameterVectorElement(θ[17]), ParameterVectorElement(θ[18]),
82+
ParameterVectorElement(θ[19]), ParameterVectorElement(θ[20]),
83+
ParameterVectorElement(θ[21]), ParameterVectorElement(θ[22]),
84+
ParameterVectorElement(θ[23])
85+
])
86+
87+
.. code-block::
88+
89+
classifier.count_ops()
90+
91+
.. parsed-literal::
92+
7293
OrderedDict([('ZZFeatureMap', 1), ('EfficientSU2', 1)])
94+
7395
"""
7496

7597
@deprecate_func(

qiskit/circuit/library/data_preparation/pauli_feature_map.py

+5
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,11 @@ def alpha(self, alpha: float) -> None:
522522

523523
@property
524524
def entanglement_blocks(self):
525+
"""The blocks in the entanglement layers.
526+
527+
Returns:
528+
The blocks in the entanglement layers.
529+
"""
525530
return [self.pauli_block(pauli) for pauli in self._paulis]
526531

527532
@entanglement_blocks.setter

0 commit comments

Comments
 (0)