diff --git a/chapter09/cirq/qnn.py b/chapter09/cirq/qnn.py index 2337452..c5b74aa 100644 --- a/chapter09/cirq/qnn.py +++ b/chapter09/cirq/qnn.py @@ -7,8 +7,7 @@ import sympy # Class for a ZX gate in Cirq -class ZXGate(cirq.ops.eigen_gate.EigenGate, - cirq.ops.gate_features.TwoQubitGate): +class ZXGate(cirq.ops.eigen_gate.EigenGate): """ZXGate with variable weight.""" def __init__(self, weight=1): @@ -20,6 +19,11 @@ def __init__(self, weight=1): self.weight = weight super().__init__(exponent=weight) # Automatically handles weights other than 1 + def _num_qubits_(self): + """Indicates the number of qubits + replaces inheriting the superclass TwoQubitGate""" + return 2 + def _eigen_components(self): return [ (1, np.array([[0.5, 0.5, 0, 0], @@ -33,7 +37,7 @@ def _eigen_components(self): ] # This lets the weight be a Symbol. Useful for parameterization. - def _resolve_parameters_(self, param_resolver): + def _resolve_parameters_(self, param_resolver, recursive=True): return ZXGate(weight=param_resolver.value_of(self.weight)) # How should the gate look in ASCII diagrams? @@ -76,7 +80,7 @@ def readout_expectation(state): # Specify an explicit qubit order so that we know which qubit is the readout result = simulator.simulate(qnn, resolver, qubit_order=[readout]+data_qubits, initial_state=state_num) - wf = result.final_state + wf = result.final_state_vector # Becase we specified qubit order, the Z value of the readout is the most # significant bit.