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

qnn.py update for most recent versions of Cirq (v0.11+) #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions chapter09/cirq/qnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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],
Expand All @@ -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?
Expand Down Expand Up @@ -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.
Expand Down