Here's a cut-down example using the Mathematica Quantum Computing library. Why is the second wildcard-based expression not matching?

state = zz075NonCommutativeTimes[xrot[zz080Operator[1]][10], xrot[zz080Operator[1]][9]];

(*This matches*)
matchSpecific[zz075NonCommutativeTimes[xrot[zz080Operator[a_]][b_], xrot[zz080Operator[c_]][d_]]] := "Great Matching";

(*This doesn't match even though it has unqualified wildcards*)
matchWildcards[zz075NonCommutativeTimes[_, _]] := "Great Wildcards"

Additionally, in this example is there a way of flagging the before___ pattern so that the zz075NonCommutativeTimes evaluation does not extract it out as part of a Times?


(*Keeps its form when it's evaluated*)
zz075NonCommutativeTimes[mygate1, mygate2, mygate3] // FullForm

When evaluated, the before___ pattern is extracted out as part of a Times
head because the evaluation assumes it's a scalar.
zz075NonCommutativeTimes[before___, mygate2, mygate3] // FullForm

This is the corresponding output:


The desired / expected output of the pattern once before___ has been tagged is:

  • $\begingroup$ It would really depend on the definition of matchWildcards. $\endgroup$ – Daniel Lichtblau Sep 13 '17 at 15:01
  • $\begingroup$ Sorry, not sure what you mean. The definition of matchWildcards is there in the code I posted, if I haven't misunderstood? $\endgroup$ – David B Sep 13 '17 at 15:32
  • 1
    $\begingroup$ I must be blind... $\endgroup$ – Daniel Lichtblau Sep 13 '17 at 16:59
  • $\begingroup$ Okay, it matched for me. I did not install or load that package though. $\endgroup$ – Daniel Lichtblau Sep 13 '17 at 17:01
  • $\begingroup$ That's right; same for me. However, if you install and load the package it won't match. I don't know, in principle, what within the package could interfere with this sort of matching. $\endgroup$ – David B Sep 13 '17 at 19:55

Your expression state has evaluated, and no longer matches the pattern.

state = 
FullForm @ state

(* Times[xrot[zz080Operator[1]][9],xrot[zz080Operator[1]][10]] *)

which won't match any pattern with Head zz075NonCommutativeTimes, unless that pattern also evaluates.

A more accessible example might be Plot, which evaluates to a Graphics when given the right arguments

 Plot[x, {x, -1, 1}],
(* False *)

 Plot[x, {x, -1, 1}],
(* True *)
  • $\begingroup$ It's documented here: reference.wolfram.com/language/tutorial/… . As always, easy to find the documentation once you know the answer ;) $\endgroup$ – David B Sep 16 '17 at 18:32
  • $\begingroup$ I can now use HoldPattern to get the rule to do what I want, but it's a bit of an ugly solution. What would be better is to stop the evaluation of zz075NonCommutativeTimes from assuming that before___ is a scalar that can be extracted into a Times. I've added text to my original question starting with "Additionally". I've read through the Notation.m code and can't work out how to communicate to Mathematica that the pattern is to be considered a QuantumObject. I appreciate that this is probably specific to the library that I'm using. $\endgroup$ – David B Sep 16 '17 at 20:43
  • $\begingroup$ This has worked for me: Unprotect[QuantumScalarQ] QuantumScalarQ[p_Pattern] := False Protect[QuantumScalarQ] $\endgroup$ – David B Sep 19 '17 at 19:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.