From 482e76e784fa258936e441d034a067b75598c7fc Mon Sep 17 00:00:00 2001 From: Matt Fishman Date: Sat, 18 Jun 2022 20:14:40 -0400 Subject: [PATCH] Fix gate parsing issue that was introduced in ITensors 0.3.14 (#292) --- Project.toml | 4 ++-- src/circuits/circuits.jl | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 8a781635..624ee58c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "PastaQ" uuid = "30b07047-aa8b-4c78-a4e8-24d720215c19" authors = ["Giacomo Torlai ", "Matthew Fishman "] -version = "0.0.23" +version = "0.0.24" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" @@ -19,7 +19,7 @@ StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" [compat] ChainRulesCore = "1.10" HDF5 = "0.13.1, 0.14, 0.15" -ITensors = "0.3.6" +ITensors = "0.3.16" JLD2 = "0.4.14" Observers = "0.0" Optimisers = "0.1.0" diff --git a/src/circuits/circuits.jl b/src/circuits/circuits.jl index 34027dd6..3261c35c 100644 --- a/src/circuits/circuits.jl +++ b/src/circuits/circuits.jl @@ -1,7 +1,40 @@ +# Specialized PastaQ parsing of a gate +# ITensors 0.3.14 (https://github.com/ITensor/ITensors.jl/pull/920) +# introduced an Op redesign, such that: +# +# Op("O", (1, 2)) +# +# get interpreted as an operator with one site with a multidimensional +# index, for example the site `(1, 2)` on a square lattice. +# +# Op("O", 1, 2) +# +# is the way to indicate a two-site operator. This is at odds with +# the PastaQ convention that: +# +# ("O", (1, 2)) +# +# or +# +# ("O", 1, 2) +# +# get interpreted as a multi-site operator with support on sites 1 and 2. +# `gate_to_op` is a wrapper around `Op` that fixes this discrepency. +gate_to_op(gate::Tuple) = gate_to_op(gate...) +gate_to_op(which_op, sites::Tuple) = Op(which_op, sites...) +gate_to_op(which_op, sites::Tuple, params::NamedTuple) = Op(which_op, sites...; params...) +gate_to_op(which_op, sites::Int...) = Op(which_op, sites...) +gate_to_op(which_op, site::Int, params::NamedTuple) = Op(which_op, site; params...) +function gate_to_op(which_op, sites_and_params::Union{Int,NamedTuple}...) + sites = Base.front(sites_and_params) + params = last(sites_and_params) + return Op(which_op, sites...; params...) +end + # This makes use of the `ITensors.Ops.Op` type, which # automatically parses a gate represented as a Tuple # into it's name, sites, and parameters. -nqubits(gate::Tuple) = maximum(Ops.sites(Op(gate))) +nqubits(gate::Tuple) = maximum(ITensors.sites(gate_to_op(gate))) nqubits(gates::Vector) = maximum((nqubits(gate) for gate in gates))